当前位置: 技术问答>java相关
变量隐藏。是一种什么概念?给个例子把
来源: 互联网 发布时间:2015-10-16
本文导语: 如题 | 先给你个例子吧,晚上在给你早资料 class Superclass { protected String x = "Super variable"; } class Subclass extends Superclass { public String x = "Sub variable"; } class Childclass extends Superclass { publ...
如题
|
先给你个例子吧,晚上在给你早资料
class Superclass
{
protected String x = "Super variable";
}
class Subclass extends Superclass
{
public String x = "Sub variable";
}
class Childclass extends Superclass
{
public String x = "Child variable";
}
public class Test
{
public static void main(String args[])
{
Subclass sc = new Subclass();
Superclass sp = sc;
Superclass ssp = new Superclass();
Childclass ch = new Childclass();
System.out.println(sc.x);
System.out.println(sp.x);//here
System.out.println(ssp.x);
System.out.println(ch.x);
}
}
class Superclass
{
protected String x = "Super variable";
}
class Subclass extends Superclass
{
public String x = "Sub variable";
}
class Childclass extends Superclass
{
public String x = "Child variable";
}
public class Test
{
public static void main(String args[])
{
Subclass sc = new Subclass();
Superclass sp = sc;
Superclass ssp = new Superclass();
Childclass ch = new Childclass();
System.out.println(sc.x);
System.out.println(sp.x);//here
System.out.println(ssp.x);
System.out.println(ch.x);
}
}