Java learning 5: rewriting of object-oriented methods & abstract classes

1. Method overloading & rewriting

1.1. What is method rewriting?

Method rewriting is used when the child class inherits the parent class and uses the parent class method! The so-called rewrite, from the point of view of program design, is to define and implement a return value type , method name , parameter list and inherited method of the parent class in the subclass are exactly the same, but the content (code) of the method implementation is different The method, which is applied to subclasses, is a manifestation of object-oriented polymorphism!
If understood from the laws of the natural world, it is the process of biological evolution and mutation. For example, assuming that the most primitive animals eat meat, then the parent's ability to eat ( that is, the eat(); method ) must be carnivorous, but with the vicissitudes of life, there are too few animals and many plants. Just eat meat. No longer adapting to the nature of society, we have to evolve animals that eat grass, so there is a subclass of herbivores , so the eat(); method of this subclass also inherits the parent class, but it can still match the parent class. Is it the same way? It must be impossible! So you need to modify the implementation of the method of the parent class !

1.2. Syntax format of method rewriting?

Insert picture description here

1.3. What should I pay attention to when rewriting the parent class method?

Insert picture description here
We will record these two principles here, and discuss and study them later!

1.4. Method overloading? The difference between rewriting and reloading?

I have said overloading this before, and it is not much different from C++. It requires different parameter lists, that is, the number of method parameters or the data types of parameters are different. This is an overload in C++, so what is the difference between Java?

Java's overloaded method:
First: the
parameter list is different, which is required.
Second:
the return value of the method can be different, which is different from C++! But the difference in return value type alone cannot distinguish overloading!
Third:
The access control symbol of the method can be different.
Fourth: The
method can be overloaded within the class and in the subclass, which shows that this is a type of protection!

The difference between rewriting and reloading?

Insert picture description here

Two, abstract class

2.1. What is an abstract class?

In class inheritance, we said that subclasses can be derived from the base class (parent class), the properties and methods of the essence of the parent class (useful for the current subclass object) can be retained, and the properties and methods suitable for the current subclass can also be expanded New methods of class objects, and even methods of overriding the parent class.
But there is also a mechanism for abstract classes in Java. This abstract class is specifically used as a parent class. This is somewhat similar to the template template of C++. The abstract class itself cannot be instantiated into an object, but can only be derived from the abstract class. The purpose is to require the designer to create and modify new classes in the format of abstract classes !

2.2. Attention to the syntax of abstract class and example demo?

Insert picture description here

Write an example: use abstract classes


public class abstractTest {
    
    
	public static void main(String[] args) {
    
    
		/********* begin *********/
		// 分别实例化Student类与Worker类的对象,并调用各自构造方法初始化类属性。
		Student s = new Student("张三", 20, "学生");

		Worker w = new Worker("李四", 30, "工人");
		// 分别调用各自类中被复写的talk()方法 打印信息。
		
		s.talk();

		w.talk();
		/********* end *********/
		
	}
}

// 声明一个名为Person的抽象类,在Person中声明了三个属性name age occupation和一个抽象方法——talk()。
abstract class Person {
    
    
	/********* begin *********/
	String name;
	int age;
	String occupation;
	Person(String name, int age, String occupation){
    
    
		this.name = name;
		this.age = age;
		this.occupation = occupation;
	}

	void talk(){
    
    
		System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",职业:" + this.occupation + "!");
	}
	/********* end *********/
}

// Student类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
class Student extends Person {
    
    
	/********* begin *********/
	Student(String name, int age, String occupation){
    
    
		super(name, age, occupation);
	}

	void talk(){
    
    
		System.out.print(this.occupation + "——>");
		super.talk();
	}
	/********* end *********/
}

// Worker类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
class Worker extends Person {
    
    
	/********* begin *********/
	Worker(String name, int age, String occupation){
    
    
		super(name, age, occupation);
	}

	void talk(){
    
    
		System.out.print(this.occupation + "——>");
		super.talk();
	}
	/********* end *********/

}

The execution result of the program:

Insert picture description here

What have we learned?

Abstract classes can write general methods, construction methods, and abstract methods!
But the abstract class itself cannot be instantiated into a concrete object, so what is its role? The abstract method of the abstract class can restrict all objects that inherit from it to have such a method, so as to avoid the loss of this class in the inheritance process The most fundamental method. Then the use of this abstract class can facilitate code reuse. Because the characteristics of each object only need to be modified a little bit in the abstract method, there is no need to write repetitive code on a large scale.

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/104892145
Recommended