class Parent {
int i = 60;
}
class Child extends Parent {
int i = 70;
}
class Main {
public static void main(String[] args) {
Child c = new Child();
Parent p = new Parent();
System.out.println((Parent c).i);
}
}
在这里,我想借助Parent
类对象打印i = 60
类变量(即Child
)。我怎么能这样做?
答案 0 :(得分:5)
System.out.println(((Parent) c).i);
用于将EXPRESSION
或VARIABLE
投射到TYPE
的简化模板是:
((TYPE)(EXPRESSION|VARIABLE))
答案 1 :(得分:1)
这是你需要的
class Parent{
int i = 60;
}
class Child extends Parent{
int i = 70;
}
class TreeMap {
public static void main(String[] args) {
Child c = new Child();
Parent p = new Parent();
System.out.println(((Parent) c).i);
}
}
答案 2 :(得分:1)
你可以写:
System.out.println(((Parent)c).i);