在Java中将子问题强制转换为父问题

在Java中将子问题强制转换为父问题,java,inheritance,casting,Java,Inheritance,Casting,在这里,我想在子类对象的帮助下打印一个父类变量(即I=60)。我该怎么做 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();

在这里,我想在
子类对象的帮助下打印一个
父类变量(即
I=60
)。我该怎么做

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);
    }

}
表达式
变量
强制转换为
类型
的简化模板为:

System.out.println(((Parent) c).i);
这是你需要的

((TYPE)(EXPRESSION|VARIABLE))
你可以写:

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);
    }

}
((父)c).我可以用super()来做吗?@MYPC,我们不能
super()
用于引用父构造函数,不参与任何表达式
System.out.println(((Parent)c).i);