java 实例化时调用了抽象方法或者class里面某个方法,如果子类有重写改方法,实际运行的是子类重写方法
package auto.test; //抽象父类
public abstract class Father {
public Father() {
// TODO Auto-generated constructor stub
oupPut(); //实例化时调用,子类有,就调用子类方法,子类没有,就调用父类方法
}
//父类抽象方法,让子类重写
public void oupPut(){
System.out.println("from Father class");
};
}
//子类A
class ChildA extends Father{
//重写父类方法
@Override
public void oupPut(){
System.out.println("from childA class");
}
}
// 子类B
class ChildB extends Father {
//重写父类方法
@Override
public void oupPut(){
System.out.println("from childB class");
}
}
class ChildC extends Father {
//没有重写父类方法 } 执行测试类
public class TestClaass { public TestClaass() {
// TODO Auto-generated constructor stub
}
public static void main(String arg[]){
Father a = new ChildA();
Father b = new ChildB();
Father c = new ChildC();
}
}
运行结果:
from childA class
from childB class
from Father class