加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用Java将子项转换为父问题

发布时间:2020-12-15 04:41:12 所属栏目:Java 来源:网络整理
导读: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); }} 这里我想借助Child类对象打印一个P
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);
    }

}

这里我想借助Child类对象打印一个Parent类变量(即i = 60).我怎样才能做到这一点?

解决方法

System.out.println(((Parent) c).i);

用于将EXPRESSION或VARIABLE转换为TYPE的简化模板是:

((TYPE)(EXPRESSION|VARIABLE))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读