编辑代码

class Main {
	public static void main(String[] args) {

        //2**change Dog or Puppy below, to see overload rule
        Puppy pet = new Puppy();
        //1**change Parent or Child below, to see inheritance rule
        Parent person = new Child();
        //look up 
        //inheritance and overload rule staticly(just checking type) locate exact one method
        person.print(pet);
        //look down
        //override,now that the only method is certain,
        //then same name methods with same para list below, is optional
        //the real type of person.print determine which to run dynamically.
        //notably,real type of pet is not consider in deciding which to run. 
	}
}

class Parent {
    void print(Puppy puppy) {
        System.out.println("parents0");
        puppy.howl();
	}
    void print(Dog dog) {
        System.out.println("parents1");
        dog.howl();
	}
    
    // void print(Animal animal) {
    //     System.out.println("parents2");
    //     animal.howl();
	// }
}

class Child extends Parent {
    void print(Puppy puppy) {
        System.out.println("Child0");
        puppy.howl();
	}
    void print(Dog dog) {
        System.out.println("Child11");
        dog.howl();
	}
    // void print(Animal animal) {
    //     System.out.println("Child2");
    //     animal.howl();
	// }
}

class Animal {
    void howl() {
        System.out.println("Animal");
	}
}

class Dog extends Animal {
    void howl() {
        System.out.println("Dog");
	}
}

class Puppy extends Dog {
    void howl() {
        System.out.println("Puppy");
	}
}