使用 Java 继承实例变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14695864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheritance instance variables with Java
提问by Rollerball
I am aware that if you subclass a class (superclass) which has for example an instance variable, it is going to be inherited in the subclass.
我知道,如果您对具有例如实例变量的类(超类)进行子类化,它将在子类中继承。
i.e. :
IE :
class Weather {
int humidity;
}
public class Rainy extends Weather {
void changeHumidity() {
humidity = 10;
System.out.println(super.humidity);
}
public static void main(String[] args) {
new Rainy().changeHumidity();
}
}
Bottom line: I would like to know why the instance variable "humidity" gets shared between subclass and class.
底线:我想知道为什么在子类和类之间共享实例变量“湿度”。
I am aware that if I shadowed that, it would not be shared but still why another instance (even though is the superclass) should share a variable with the subclass in the inheritance logic.
我知道如果我隐藏它,它不会被共享,但为什么另一个实例(即使是超类)应该与继承逻辑中的子类共享一个变量。
Thanks in advance.
提前致谢。
回答by Nathan Hughes
There were two decisions the creators of Java took that guided how they designed Java. One was that performance was a priority, it must not be judged unusable due to being too slow. The other decision was that they would target C and C++ developers, and make Java similar to what they were used to in order to make adoption easier.
Java 的创建者做出了两个决定来指导他们如何设计 Java。一个是性能是优先考虑的,不能因为太慢而被判断为无法使用。另一个决定是他们将针对 C 和 C++ 开发人员,并使 Java 与他们习惯的相似,以便更容易采用。
So Java got public, private, and protected access like C++. Where package-private came in and why it is the default is not clear. They might have assumed developers would want to access variables directly in order to save on method call overhead. A long time ago I was working on a project with professional services people from Marimba, and we had the opportunity to investigate some of the implementation code, which was written with a lot of package-private members. When I asked why the explanation was it was for performance. This was before optimizations like JIT, so there may have been a concern that using accessor methods to get to frequently used superclass members might be too slow. On the other hand it might have been a style preference or they were using a freer approach as part of an evolving design, and maybe the idea behind making package-private the default was that this kind of freedom should be expected. (It's not something that has caught on judging by any other code I've read.) It was code written by Jonathon Payne and Arthur Van Hoff, so the professional service people may not have been in on the real reasons.
因此,Java 像 C++ 一样获得了公共、私有和受保护的访问权限。package-private 在哪里出现以及为什么它是默认值尚不清楚。他们可能假设开发人员希望直接访问变量以节省方法调用开销。很久以前,我与来自 Marimba 的专业服务人员一起从事一个项目,我们有机会调查一些实现代码,这些代码是由许多包私有成员编写的。当我问为什么解释是为了性能时。这是在像 JIT 这样的优化之前,所以可能有人担心使用访问器方法来获取经常使用的超类成员可能太慢。另一方面,它可能是一种风格偏好,或者他们正在使用更自由的方法作为不断发展的设计的一部分,也许将 package-private 设为默认设置背后的想法是应该期待这种自由。(从我读过的任何其他代码来看,这不是什么东西。)它是由 Jonathon Payne 和 Arthur Van Hoff 编写的代码,所以专业服务人员可能没有真正了解真正的原因。
There's an interview where James Gosling talks about his reasons:
... one of the things that I had wanted to do early on was formalize setting and getting something in the language. You know how in JavaBeans there's this sort of convention that you write setter and getter methods? But I did a bunch of surveys of developers at the time, about whether or not they would like this to be there. And the average person went, "Oh my God!"
And so I didn't do it. But I think in retrospect, I should never have listened to them. I should have just done it. Because, I mean Beans basically layered on this facility that I had wanted to do anyway, but Beans did it as kind of an afterthought.
And because it's layered on as a naming convention, there's some things that don't fit together real well. And so, for instance, it would've made a lot of sense for the default protection for an instance variable to be private. And then the getters and setters, which the system would've known about at a much deeper level, to make them either public or package.
......我很早就想做的一件事是正式化设置并在语言中获得一些东西。您知道在 JavaBeans 中如何编写 setter 和 getter 方法的这种约定吗?但是我当时对开发人员进行了大量调查,询问他们是否愿意在那里使用它。普通人会说,“天哪!”
所以我没有这样做。但我回想起来,我不应该听他们的。我应该刚刚完成。因为,我的意思是 Beans 基本上是在我一直想做的这个设施上分层的,但是 Beans 是事后才做的。
而且因为它是作为命名约定分层的,所以有些东西不能很好地结合在一起。因此,例如,将实例变量的默认保护设为私有是很有意义的。然后是系统在更深层次上知道的 getter 和 setter,以将它们设为公开或打包。
The direction in which the style has evolved has been to prefer making instance variables private, and expose them through getters if required. You can specify the private access modifier, like this:
风格演变的方向是倾向于将实例变量设为私有,并在需要时通过 getter 公开它们。您可以指定私有访问修饰符,如下所示:
private int humidity;
and then the variable won't be visible to the subclass.
然后该变量对子类不可见。
回答by Peter Lawrey
I would like to know why the instance variable "humidity" gets shared between subclass and class.
我想知道为什么在子类和类之间共享实例变量“湿度”。
That is what default/package local members do.
这就是默认/包本地成员所做的。