Java learning (super detailed) eight (polymorphism)

Table of contents

1. Case

2. Polymorphic definition

3. Three elements of polymorphism

4. Polymorphic representation

5. Upcasting in Polymorphism

6. Downcasting in polymorphism

7. instanceof operator


1. Case

Case 1 : The pet is hungry, and different pets eat different things. Dogs eat dog food, penguins eat fish

Here we can use the inheritance method we learned before to define a Pet class first, and then implement the Dog class and Penguin class respectively.

public abstract class Pet {
    public abstract void eat();
}
public class Dog extends Pet {
    public void eat() {
        System.out.println("狗粮");
    }
}
public class Penguin extends Pet {
    public void eat() {
        System.out.println("鱼");
    }
}

 Case 2 : What if the owner needs to feed the pet?

Add the Master class to implement the method of feeding feed(xx) to XXX

public class Master {
    public void feed(Dog dog){
        System.out.println("喂食...");
        dog.eat();
    }
    public void feed(Penguin penguin){
        System.out.println("喂食...");
        penguin.eat();
    }
}

Case 3 : What should I do if adding feed() multiple times in the Master class leads to code redundancy?

Modify the Master class and add the feed(Pet pet) method

public class Master {
    public void feed(Pet pet){
        System.out.println("喂食...");
        pet.eat();
    }
}

Case 4 : Realize the owner adopts a pet through a small card

Idea: Add Pet getPet(int typeId) to Master

public class Master {
    public void feed(Pet pet){
        System.out.println("喂食...");
        pet.eat();
    }
    public Pet getPet(int typeId){
        Pet pet = null;
        if (typeId == 1){
            pet = new Dog();
        }else if(typeId == 2){
            pet = new Penguin();
        }
        return pet;
    }
}

Case 5 : Realize the function of playing between the owner and the pet, play the Frisbee game with the dog, and play the swimming game with the penguin 

Add the catchFlyDisc method to Dog catchingFlyDisc( )

Add swimming method swimming( ) to Penguin

Add the play(Pet pet) method to the master

public class Dog extends Pet {
    public void eat() { System.out.println("狗粮"); }
    public void catchingFlyDisc(){
        System.out.println("接飞盘");
    }
}
public class Penguin extends Pet {
    public void eat(){ System.out.println("鱼"); }
    public void swimming(){ System.out.println("游泳"); }
}
public class Master {
    public void feed(Pet pet){
        System.out.println("喂食...");
        pet.eat();
    }
    public Pet getPet(int typeId){
        Pet pet = null;
        if (typeId == 1){
            pet = new Dog();
        }else if(typeId == 2){
            pet = new Penguin();
        }
        return pet;
    }
    public void play(Pet pet){
        if (pet instanceof Dog){
            ((Dog) pet).catchingFlyDisc();
        }else if(pet instanceof Penguin){
            ((Penguin) pet).swimming();
        }
    }
}

2. Polymorphic definition

The definition of polymorphism in life: the same operation, due to different conditions, produces different results. For example, different fruits are put into the juicer, and the squeezed juice is also different.

Polymorphic definition in computers: the parent class reference points to the subclass object, just like the parent class in the above case as a formal parameter

3. Three elements of polymorphism

1. Write the parent class and subclass with inheritance relationship

public class Dog extends Pet

2. The subclass overrides the parent class method

public abstract class Pet {
    public abstract void eat();
}
public class Dog extends Pet{
    public void eat(){
        System.out.println("狗粮");
    }
}

 3. Use the reference of the parent class to point to the object of the subclass

    public Pet getPet(int typeId){
        Pet pet = null;
        if (typeId == 1){
            pet = new Dog();
        }else if(typeId == 2){
            pet = new Penguin();
        }
        return pet;
    }

4. Polymorphic representation

1. Use the parent class as a method parameter

    public void feed(Pet pet){
        System.out.println("喂食...");
        pet.eat();
    }

2. Use the parent class as the method return value

    public Pet getPet(int typeId){
        Pet pet = null;
        if (typeId == 1){
            pet = new Dog();
        }else if(typeId == 2){
            pet = new Penguin();
        }
        return pet;
    }

5. Upcasting in Polymorphism

  1. Definition: The subclass is converted to the parent class, that is, the subclass object is referenced by the reference variable of the parent class
  2. Features: automatic conversion
  3. Code embodiment:
    public Pet getPet(int typeId){
        Pet pet = null;
        if (typeId == 1){
            pet = new Dog(); //子类Dog转换成父类Pet
        }else if(typeId == 2){
            pet = new Penguin();
        }
        return pet;
    }

6. Downcasting in polymorphism

  1. Definition: The parent class is converted into a subclass. Because the type of the parent class is referenced, only the method attributes defined in the parent class can be used. To use the method attributes in the subclass, a forced conversion is required
  2. Features: forced conversion
  3. Code embodiment:
    public void play(Pet pet){
        if (pet instanceof Dog){
            ((Dog) pet).catchingFlyDisc();//强制转换
        }else if(pet instanceof Penguin){
            ((Penguin) pet).swimming();
        }
    }

7. instanceof operator

This operator has already appeared in the above case, I believe the smart friends have already guessed its function

Definition: Pet instanceof Dog judges whether the Pet type belongs to the Dog type

Guess you like

Origin blog.csdn.net/jojo_oulaoula/article/details/131214715