[讨论]多态问题
class A {
String s="class A";
void show()
{
System.out.println("A1");
}
}
class B extends A
{
String s="class B";
//String s1="class B1";
void show()
{
System.out.println("B1");
}
}
class TypeConvert
{
public static void main(String args[])
{
B b1,b2=new B();
A a1,a2;
a1=(A)b2;
a2=b2;
System.out.println(a1.s);
System.out.println(a2.s);
a1.show();
b1=(B)a1;
//System.out.println(b1.s1);
System.out.println(b2.s);
System.out.println(b2.s);
b1.show();
}
}
结果:
class A
class A
A1
A1
class B
class B
B1
B1
为什么回有如此结果?
讨论.....