英文:
Why can I change the private attribute in the class?
问题
public class Drink {
private String contents;
Drink(String theContents){
contents = theContents;
}
public static void main(String[] args) {
Drink water = new Drink("water");
Drink oj = new Drink("orange juice");
Drink cocaCola = new Drink("Coca Cola");
oj.contents = "not orange juice";
System.out.println(oj.contents);
}
}
我认为输出应该会产生一个错误。
例如这一行:oj.contents = "not orange juice";
应该会产生一个错误。
如果我理解错了,请告诉我为什么
谢谢
英文:
I am using Java
public class Drink {
private String contents;
Drink(String theContents){
contents = theContents;
}
public static void main(String[] args) {
Drink water = new Drink("water");
Drink oj = new Drink("orange juice");
Drink cocaCola = new Drink("Coca Cola");
oj.contents = "not orange juice";
System.out.println(oj.contents);
}
}
I thought the output should be an error.
e.g.the line oj.contents = "not orange juice";
should produce an error.
If this is wrong please tell me why
thanks
答案1
得分: 2
你只有一个名为Drink的类,其中包含变量和主方法。因此,主方法可以访问私有变量,因为它们在同一个类中。如果你想要从另一个类中访问这个变量,你需要使用getter和setter方法。
英文:
You have only one class called Drink which contains the variable and the main method. Therefore the main method can access the private variable, because they are in the same class. If you would want to access the variable from another class, you would need getter and setter methods to do so.
答案2
得分: 2
Public variables, are variables that are visible to all classes.
Public(公共)变量是对所有类可见的。
Private variables, are variables that are visible only to the class to which they belong.
Private(私有)变量只对其所属的类可见。
Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.
Protected(受保护)变量只对其所属的类和任何子类可见。
variable content is not visible to other classes because it is private. But you can access it anywhere in the class Drink.
变量content不对其他类可见,因为它是私有的。但在类Drink中,您可以随时访问它。
英文:
Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.
variable content is not visible to other classes because it is private. But you can access it anywhere in the class Drink
答案3
得分: 0
private
大多数时候表示:对同一源代码文件可见(因此可编辑;private/public/package-private/protected 都是一种全有或全无的情况)。
以下是一个我认为不错的理解方式:代码是与类一起使用的概念;而不是与这些类的实例一起使用的。因此,在这里您的代码正在修改其自身的一个字段:contents
是此源代码文件的一个私有字段。"但是,它不是你自己的,它是来自饮料 oj
!" - 不对,那是基于对象的思维。基于类的思维是:它是字段 "Drink.contents",位于此文件中。
英文:
private
mostly means: Visible to the same source file (and therefore editable; private/public/package-private/protected are all an all-or-nothing affair).
Here's a good (IMO) way to think about it: Code is a concept that goes with classes; not instances of those classes. Therefore, your code here is modifying one of its own fields: contents
is a private field of this very source file. "But, it's not your own, it is from drink oj
!" - nope, that's object-based thinking. class-based thinking says: It's the field "Drink.contents", which is in this file.
答案4
得分: 0
从官方文档:
这张图片会为您总结一切:
任何使用“private”访问修饰符标记的方法或变量都可以被定义它的类访问。任何类外部的组件都无法访问。
尽管您还应该了解一下Java中的反射,这是对此规则的一个例外。
英文:
From the official docs
This picture will sum it up for you:
Any method or variable marked with the "private" access modifier is accessible to the class where it is defined. Any component outside of that class won't be able to access.
Though you should also check about Reflection in java which an exception to this rule.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论