Java中的main可以访问类中的私有变量吗?
我最近开始使用JDK1.6学习Java。如果这是一个愚蠢的问题,请原谅。
如果私有变量可以被 main() 中的对象直接访问,它们如何是“私有”的?
public class Account1
{
private int accountNum;
private String name;
Account1() {
accountNum = 1101;
name = "Scott";
}
public void showData() {
System.out.println("Account Number: " + accountNum +
"\nName: " + name);
}
public static void main(String[] args) {
Account1 myA1 = new Account1();
myA1.showData();
System.out.println(myA1.accountNum); //Works! What about "Private"?!
}
}
给出输出:
Account Number: 1101
Name: Scott
1101
I've recently started learning Java using JDK1.6. If this is a silly question, please excuse me.
If private variables can be directly accessed by objects in main() how are they 'private'?
public class Account1
{
private int accountNum;
private String name;
Account1() {
accountNum = 1101;
name = "Scott";
}
public void showData() {
System.out.println("Account Number: " + accountNum +
"\nName: " + name);
}
public static void main(String[] args) {
Account1 myA1 = new Account1();
myA1.showData();
System.out.println(myA1.accountNum); //Works! What about "Private"?!
}
}
Which gives the output:
Account Number: 1101
Name: Scott
1101
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 main 位于 Account1 类中,因此它仍然在同一范围内。
私有变量可以从属于同一类型的任何代码访问。如果您的主要方法位于单独的类中,那么它将无法访问它们(不使用反射)。
Your main is in the Account1 class, so it's still in the same scope.
Private variables can be accessed from any code belonging to the same type. If your main method was in a separate class then it wouldn't be able to access them (without using reflection).
给定类的“main”方法是该类的一部分。属于类的方法可以访问该类的私有成员。这对我来说很有意义。当然,并不一定意味着您应该使用它。
思考这个问题的一种方法是考虑一个类对另一个类内部运作的了解。我的 Person 类不应该知道我的 Order 类中发生了什么;它只是调用公共方法。但是 Person 内部的任何东西当然都会知道 Person 的内部结构——甚至是 Person 的不同实例。
The "main" method of a given class is part of that class. Methods that are part of a class have access to private members of that class. That makes sense to me. Doesn't necessarily mean you should use it, of course.
One way to think about it is to think about one class's knowledge of another class's internal workings. My Person class shouldn't know what happens inside my Order class; it just calls public methods on it. But anything inside Person will of course know about the internal structure of Person -- even a different instance of Person.
这是因为 main() 函数是该类的成员。它可以访问班级的所有成员。
在现实世界的代码中,主函数通常位于“harness”类中,该类实际上引导其余的代码。该线束类通常非常轻量级,并实例化执行实际工作的其他类。
This is because the main() function is a member of the class. It has access to all members of the class.
In real world code, the main function is usually situated in a "harness" class that actually bootstraps the rest of the code. This harness class is usually very lightweight and instantiates other classes that do the real work.
它们是私有的,因为它们只能由该类访问。这意味着它们可以从该类的静态方法(例如
main
)以及实例方法(例如showData
)访问。该类的一个实例也可以访问该类的另一实例的私有成员。
如果您有一个单独的类,例如
Account2
,它将无法访问Account1
的 provate 成员。They are private in that they can only be accessed by that class. This means they are accessible from static methods of that class (such as
main
) and also from instance methods (such asshowData
).One instance of the class can also access private members of another instance of the class.
If you had a separate class, say,
Account2
, it would not be able to access provate members ofAccount1
.