简体   繁体   English

可以从Java中的main访问类中的私有变量吗?

[英]Private variables in a class can be accessed from main in Java?

I've recently started learning Java using JDK1.6. 我最近开始使用JDK1.6学习Java。 If this is a silly question, please excuse me. 如果这是一个愚蠢的问题,请原谅。

If private variables can be directly accessed by objects in main() how are they 'private'? 如果私有变量可以被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"?!
}
}

Which gives the output: 给出输出:

Account Number: 1101  
Name: Scott  
1101

Your main is in the Account1 class, so it's still in the same scope. 您的主要帐户位于Account1类中,因此它仍在同一范围内。

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). 如果您的主要方法在单独的类中,那么它将无法访问它们(不使用反射)。

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; 我的Person类不应该知道我的Order类内部会发生什么; 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. 但是,人内部的任何内容当然都会知道人的内部结构-甚至是人的不同实例。

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 as showData ). 这意味着可以从该类的静态方法(例如main )和实例方法(例如showData )中访问它们。

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 of Account1 . 如果您有一个单独的类,例如Account2 ,则它将无法访问Account1正式成员。

This is because the main() function is a member of the class. 这是因为main()函数是该类的成员。 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. 这个线束类通常是非常轻量级的,并且会实例化执行实际工作的其他类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM