Polymorphism is running behavior!
The certificate is as follows:
import java.util.Random; public class InterviewTest { public static Animal getInstance(int key) { switch(key) { case 0: //If key=0 return new Cat(); //Return to the newly created object (CAT) case 1: return new Dog(); default: return new Pig(); } } public static void main(String[] args) { int key = new Random().nextInt(3); //012 random input System.out.println(key); //Print key Animal animal = getInstance(key); //Create an object based on the value of the key animal.eat(); //Call the eat() method animal.shout(); //Call the shot() method } } //Create animal class class Animal{ public void eat() { System.out.println("Animals: eating"); } public void shout() { System.out.println("Animal: call"); } } //Create a subclass dog, inherit the animal class, and override the method class Dog extends Animal{ public void eat() { //Method rewrite System.out.println("Dogs eat bones"); } public void shout() { System.out.println("Woof! Woof! Woof!"); } } class Cat extends Animal{ public void eat() { System.out.println("Cats eat fish"); } public void shout() { System.out.println("Meow! Meow! Meow!"); } } class Pig extends Animal{ public void eat() { System.out.println("Pigs eat everything"); } public void shout() { System.out.println("Pig, pig! Pig, pig! Pig, pig!"); } }
The running result is random, such as 2. The above programs also have polymorphism during compilation, but the program can only decide which object method to call at runtime. The above program determines to call the corresponding object and its method according to the randomly given key value at runtime. When key=2, create a pig object and call eat() and shoot() methods.
2 Pigs eat everything Pig, pig! Pig, pig! Pig, pig!
Polymorphism can be understood as multiple forms of a thing.
For example, there is an Animal class and a dog class. Dog inherits the Animal class and overrides its methods. Cat, Pig and other classes can also be written, which is omitted here.
class Animal{ public void eat() { System.out.println("Animals: eating"); } public void shout() { System.out.println("Animal: call"); } } class Dog extends Animal{ public void eat() { System.out.println("Dogs eat bones"); } public void shout() { System.out.println("Woof! Woof! Woof!"); } public void tail(){ System.out.println("The dog wagged its tail~"); } } class Cat extends Animal{ ... public void shout() { System.out.println("Meow! Meow! Meow!"); } ... }
At this point, you can create a Dog class with the following methods, which is the polymorphism of the object.
Animal animal = new Dog();
Create a test class t1 and a method func.
public class AnimalTest { public static void main(String[] args) { AnimalTest t1 = new AnimalTest(); t1.func(new Dog()); t1.func(new Cat()); } //Use polymorphism to eliminate method overloading //Animal animal = new Dog(); public void func(Animal animal) { animal.eat(); animal.shout(); } }
When a subclass calls a method, only the method of the parent class can be called at compile time. The runtime is actually the overridden method in the subclass. The running results of the above code are as follows. The methods of subclasses are called.
Dogs eat bones Woof! Woof! Woof! Cats eat fish Meow! Meow! Meow!
The parent class Animal has eat() and shoot() methods, and the child Dog has tail() methods, which cannot be called during compilation. The error "The method tail() is undefined for the type Animal" will be reported. That is, this method is not defined in the Animal class.
- Object polymorphism: the reference of the parent class points to the object of the child class;
Animal animal = new Dog();
- Use of polymorphism: virtual method calls
With the polymorphism of the object, only the methods declared in the parent class can be called at compile time; At run time, what is actually running is the method that the child class overrides the parent class. Compile to the left (parent), run to the right (child).
Animal animal = new Dog(); animal.eat(); animal.shout();
Dogs eat bones Woof! Woof! Woof!
- Prerequisites for polymorphic use: ① inheritance relationship of classes ② method rewriting
- Object polymorphism applies only to methods, not attributes. (see the left for compilation and operation)
Create attribute ID for both parent and child classes, where Animal class int id = 1;, Dog class int id = 2;.
Animal animal = new Dog(); System.out.println(animal.id);//1
The above content comes from Mr. Song Hongkang's Java course arrangement.