class A{
int a0;
void funA(){
System.out.println("funA()");
}
}
class B extends A {//B继承A
int b0;
void funB(){
System.out.println("funB()");
}
}
public class TestDemo{
public static void main (String args[]){
A b = new B();//向上转型为A
if(b instanceof B){
B newb = (B)b;//向下转型为B
newb.funA();
newb.funB();
System.out.println(newb.b0);
System.out.println(newb.a0);
}
}
}
向下转型
最新推荐文章于 2024-07-12 21:24:51 发布