package duotaidemo;
/*
* 写了一个对象向下转型的demo
* 总结:对象向下转型必须先向上转型,向上转型是为了确定身份,知道父类还有这么一个儿子,所有
* 才能向下转型
*/
public class duotaidemo {
public static void main(String[] args) {
//向下转型之前必须要先向上转型
//向上转型
A a = new B();
//向下转型
B b = (B)a;
b.fun();
b.fun2();
b.fun3();
}
}
//写一个父类
class A{
//写一个方法
public void fun(){
System.out.println("hello");
}
//再写一个方法,调用本对象的fun方法
public void fun2(){
this.fun();
}
}
//写一个子类去继承父类A
class B extends A{
//复写父类中的fun方法
public void fun(){
System.out.println("hello+hai!!");
}
//写一个子类独有的方法
public void fun3(){
System.out.println("only me!!!");
}
}