JAVA-------Detailed explanation of polymorphism

Polymorphism: the same object shows different forms at different moments

  • Example: cat
    we can say that a cat is a cat: Cat cat = new Cat ();
    we can also say that the cat is an animal: Animal animal = new Cat ();
    Here Cat in different times showed a different state , which is polymorphic

  • The premise and realization of polymorphism:
    1. Inheritance/implementation relationship
    2. There are ways to rewrite
    3. Have a parent class reference pointing to a subclass object

  • The benefits of polymorphism:

    • When defining the method, use the parent type as a parameter, and use the specific subtype to participate in the operation when it is used in the future
  • Disadvantages of polymorphism:

    • Cannot use the unique functions of the subclass

Look at the code below

animal class:

public class animal {
    
    
	
	public int age=40;
	
	public void eat() {
    
    
		System.out.println("动物吃东西");
	}
}

cat class: rewrite animal

public class cat extends animal {
    
    
	
	public int age=20;
	public int weight=10;
	
	@Override
	public void eat() {
    
    
		// TODO Auto-generated method stub
		System.out.println("猫吃鱼");
	}

	public void playgame() {
    
    
		System.out.println("猫捉迷藏");
	}
}

Dog class: rewrite animal

public class dog extends animal {
    
    
	@Override
	public void eat() {
    
    
		System.out.println("狗吃骨头");
	}
}

animalOperation class:

public class animalOperation {
    
    
	public void useAnimal(cat c) {
    
    
		c.eat();
	}
	
	public void useAnimal(dog d) {
    
    
		d.eat();
	}
	}
}

main main class:

public class animalDemo {
    
    
	public static void main(String[] args) {
    
    
		//有父类引用指向子类对象
		animal a=new cat();
		
		//多态访问成员变量编译和运行都要看animals有没有
		System.out.println(a.age);//输出40

		//多态访问成员方法编译看左边,运行看右边
		a.eat();
		
		//创建动物操作类对象,调用方法
		animalOperation ao=new animalOperation();
		cat c=new cat();
		ao.useAnimal(c);
		
		dog d=new dog();
		ao.useAnimal(d);
		
	}
}

Here we need to pay attention to the way that polymorphism accesses member variables and member methods! !

For example, create a polymorphic class here, with the animal class on the left, and the cat class on the right is a subclass of the animal class.

animal a=new cat();
  • At this time, when polymorphic access to member variables, whether it is compiled or run, it depends on whether there is any animal in the class. Therefore, when accessing member variables, compile and run are based on the left side.

  • When polymorphic access to member methods, the compiler looks at animal, and when it runs, it looks at cat. That is to say, at the time of compilation, it looks at whether the animal on the left has this method, and when it runs, it looks at the method in cat on the right. Therefore When accessing member methods, compile and look at the left, and run to look at the right


And the advantage of multi-banding is: when defining the method, use the parent type as a parameter, and use the specific subtype to participate in the operation when it is used in the future.

You can see that every time you define a new class in the animalOperation class above, you need to rewrite the method of the class. Polymorphism solves this problem. You can change the above code to this, and use the parent class as the parameter. , And the subclass can be used directly when calling.


public class animalOperation {
    
    
	
	public void useAnimal(animal a) {
    
    
		//等同于animal a=new cat();
		//animal a=new dog();
		a.eat();
	}
}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113122246