package project3;public class Animal { public int age; public String name; public void eat() { System.out.println("动物具有吃东西的能力!"); } public Animal() { System.out.println("Animal类执行!"); }}
package project3;public class Dog extends Animal { public Dog() { System.out.println("Dog类执行!"); }}
package project3;public class Inil { public static void main(String[] args) { // TODO Auto-generated method stub Dog dog = new Dog(); dog.age = 10; dog.name = "hh"; dog.eat(); }}
求解答!!!!!
你这是重写了吧,子类跟父类的方法名一样,会优先使用子类方法,而不会执行父类。作为小白是这么理解的
知道原因了