- 论坛徽章:
- 0
|
public class Son extends Father
{
public boolean flag = true;
public void print()
{
System.out.print("son.print()");
System.out.print("Son's flag:"+this.flag);
}
public static void main(String[] args)
{
Father f = new Son();
System.out.println("f.flag = "+((Son)f).flag); //转换成子类型后,获取的是子类型的成员变量
System.out.println("f.flag = "+f.flag); //默认获取声明类型Father的成员变量;
f.print();
}
}
class Father
{
public boolean flag = false;
protected void print()
{
System.out.print("father.print()");
System.out.println("father's flag"+this.flag);
}
} |
引用类型不同而已; 多看看C++.. |
|