java变量不可读为什么,为什么Java中类的成员变量不能被重写?

为什么Java中类的成员变量不能被重写?

这篇文章讨论了Java面向对象概念中一个基本的概念--Field Hiding(成员变量隐藏)

成员变量在Java中能够被重写么?

Let’s first take a look at the following example which creates two Sub objects. One is

assigned to a Sub reference, the other is assigned to a Super reference.

我们看下面这个例子,我们创建了两个子对象,一个使用的是子对象的引用,一个使用的是父对象的引用。

public class FieldOverriding {

public static void main(String[] args) {

Sub c1 = new Sub();

Super c2 = new Sub();

System.out.println(c1.s);

System.out.println(c2.s);

}

}

class Super {

String s = "Super";

}

class Sub extends Super {

String s = "Sub";

}

程序的输出结果是:

Paste_Image.png

按照我们已有的多态的概念,第二个应该是输出sub才对,但却输出了super。这是为什么呢?

不会重写成员变量,而是隐藏成员变量

Java文档中对隐藏域的定义:

Within a class, a field that has the same name as a field in the superclass hides the superclass’s field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super. Generally speaking, we don’t recommend hiding fields as it makes code difficult to read.

意思就是:

在一个类中,子类中的成员变量如果和父类中的成员变量同名,那么即使他们类型不一样,只要名字一样。父类中的成员变量都会被隐藏。在子类中,父类的成员变量不能被简单的用引用来访问。而是,必须从父类的引用获得父类被隐藏的成员变量,一般来说,我们不推荐隐藏成员变量,因为这样会使代码变得难以阅读。

其实,简单来说,就是子类不会去重写覆盖父类的成员变量,所以成员变量的访问不能像方法一样使用多态去访问。

访问隐藏域的方法

就是使用父类的引用类型,那么就可以访问到隐藏域,就像我们例子中的代码

就是使用类型转换System.out.println(((Super)c1).s);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值