Java study notes-method coverage and polymorphism

Review method overload

When in a class, if the functions are similar, it is recommended to define the name the same,
so that the code is beautiful and easy to program.

What conditions can constitute method overloading?

  • In the same class
  • Method names are the same
  • Different parameter lists (number, order, type)

Method Override

After the subclass inherits the parent class, when the inherited method cannot meet the business needs of the current subclass, the
subclass has the right to rewrite this method, and it is necessary to "override the method".

Method coverage is also called: method override (Override Overwrite)

When we write code, how does it form method coverage at the code level?

  1. The two classes must have an inheritance relationship.
  2. The rewritten method has the same return value type, method name, and formal parameter list as the previous method
  3. The access permission cannot be lower, but can be higher (for method coverage in the subclass, the access permission must be higher than or equal to the corresponding method in the parent class; remember first)
  4. The rewritten method cannot throw more exceptions than the previous method, it can be less (remember first)

Precautions:

  • Method coverage is only for methods, and has nothing to do with attributes
  • Private methods cannot be overridden
  • Constructors cannot be inherited, so constructors cannot be overridden
  • Method coverage is only for instance methods, "static method coverage" is meaningless.

Sample code:

class Animal{
	public void move(){
		System.out.println("动物在行走");
	}
}

class Cat extends Animal{
	//方法覆盖
	public void move(){
		System.out.println("猫在走猫步");
	}
	//方法重载
	public void move(int a){
		System.out.println("猫在走猫步");
	}
}

public class OverrideTest{
	public static void main(String[] args){
		Cat c = new Cat();
		c.move(3);
		c.move();
	}
}

Explanation: There is no method coverage for static methods

First of all, method coverage and polymorphism mechanism are inseparable. Method coverage serves polymorphism mechanism
and polymorphism is naturally related to objects, and the implementation of static methods does not require objects.
So, in general, we will say that "static method coverage" is meaningless , The static method "does not exist" method overrides,
so call the static method must use the "class name." Way, do not use "reference." Caused ambiguity

Sample code:

public class Test{
	public static void main(String[] args){
		//多态
		Pet p = new Cat();
		//静态方法和对象无关,只和类相关
		//虽然静态方法可以使用"引用."的方式调用,但是实际运行时还是Pet.doSome();
		p.doSome();
		
		Pet.doSome();
		Cat.doSome();
	}			
}

class Pet {
	static public void doSome() {
		System.out.println("Pet类的doSome方法执行");
	}
}

class Cat extends Pet{
	static public void doSome() {
		System.out.println("Cat类的doSome方法执行");
	}
}

Explanation: Private methods cannot be overwritten (remember)

Sample code:

public class OverrideTest{
	
	//私有方法
	private void doSome(){
		System.out.println("OverrideTest类的私有方法执行");
	}
	public static void main(String[] args){
		OverrideTest ot = new T();
		ot.doSome();
	}
}

class T extends OverrideTest{
	// 尝试重写父类中的doSome()
	// 子类方法访问权限不能更低,可以更高
	public void doSome(){
		System.out.println("T类的方法执行");
	}
}

Explanation: About the return value type in method override

The return value type "reduced" in the subtype method coverage is grammatically allowed,
but the method coverage used in actual development is to directly copy and paste the method in the parent type. Big

Sample code:

public class Test{
	public static void main(String[] args){
		
		Pet p = new Cat();
		p.doSome();
	}			
}
class Pet {
	public Pet doSome() {
		System.out.println("Pet类的doSome方法执行");
		return null;
	}
}

class Cat extends Pet{
	public Cat Pet doSome(){
		System.out.println("Cat类的doSome方法执行");
		return null;
	}
}

Guess you like

Origin www.cnblogs.com/zy200128/p/12682806.html