如果我有如下三个类:
package com.Bob.Marley;
public class SuperClass{
protected int x = 0;
}
package com.Bob.Marley;
public class SubClass extends SuperClass{
protected int x = 1;
}
package com.Bob.Marley;
public class TestClass{
public static void main (String[] args){
SubClass s = new SubClass();
//print 1
System.out.println(s.x);
//how do I print the superclass variable?
//I know inside SubClass I can access it with plain old super.x
//but what about outside the subclass with a new object.
}
}
所以问题是我如何从在单独的类中创建的新对象的超类中打印出 0 。System.out.println(s.super.x); 不起作用。我认为它不会改变任何东西,但我使用的是 java 8。
慕虎7371278
开心每一天1111
相关分类