为什么“私人”可以方法可以从不同的实例访问? [英] Why can a "private" method be accessed from a different instance?

查看:156
本文介绍了为什么“私人”可以方法可以从不同的实例访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这是一个非常基本的代码,但似乎Java中存在一些根本性的缺陷,或者我用来运行代码的Eclipse IDE使用的JVM。

Although, this is a very basic code, it seems there is something fundamentally flawed in Java, or the JVM used by the Eclipse IDE I have used to run the code.

代码运行即使它不应该(我认为)! A.java中的代码只显示你好,我是A!

The code runs even though it should not (I think)! The code in A.java simply displays "Hello, I am A!"

这是:

import java.lang.*;
import java.util.*;
class A {   
      private void methodA() {System.out.println("Hello, I am A!");}   
      public static void main(String[] args) {   
        A a = new A();   
        a.methodA(); }   
} 

在创建A类实例后,我不明白为什么()成功在该实例上运行A类的私有方法。是的,main方法属于A类,但它不是在this引用的上下文中从 inside 当前对象访问私有方法。实际上,由于它是静态方法,因此无法从类中访问非静态成员。而不是main(),非静态成员方法可以仅从内部调用methodA()。但这是另一个问题,因为我没有定义任何非静态的第二种方法。

I do not understand why, after creating an instance of class A, main() succeeds in running a private method of class A on that instance. Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference. In fact, since it is a static method, it cannot access non-static members from within the class. Instead of main(), a non-static member method could have invoked methodA() from inside only. But that is another issue since I have not defined any non-static second method.

现在谈到了内部视图,让我们来看看回到这一点,外部视野。如您所见,main()尝试从外部对象调用methodA并成功!为什么私有被视为私有

Now that the inside-view is talked about, let's come back to the point, the outside-view. As you can see, main() attempts to invoke methodA from outside the object and succeeds! Why isn't private getting treated as private?

我正在拉我的头发......

I am pulling my hair....

任何人,请回复......

Anyone, please reply...

推荐答案


是的,main方法属于A类,但它不是在this引用的上下文中从当前对象内部访问private方法。

Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference.

这没关系。这不是Java使用的可访问性模型。重要的是编写代码的类,而不是它是否访问同一对象中的成员。

That doesn't matter. That's just not the model of accessibility that Java uses. What's important is the class in which the code is written, not whether it's accessing members in the same object.

这是非常有用的,否则(例如)它会使用这两个类的私有成员是不可能实现等于的方法。

This is very useful, as otherwise (for example) it would be impossible to implement an equals method using private members of both classes.

这可能不是你的方式选择指定语言,但指定Java的方式。请参见 JLS 6.6.1 有关辅助功能的详细信息。特别是:

This may not be how you would have chosen to specify the language, but it is how Java is specified. See JLS 6.6.1 for details of accessibility. In particular:


否则,如果成员或构造函数被声明为私有,那么当且仅当它发生在主体内时才允许访问包含成员或构造函数声明的顶级类(第7.6节)。

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

注意 protected 访问稍微奇怪 - 类 SuperClass 的受保护实例成员只能在<$ c中的代码中访问$ c> SubClass 通过表达式 SubClass 或另一个子类。但它仍然不必是相同的对象。

Note that protected access is slightly odd here - a protected instance member of class SuperClass can only be accessed within code in SubClass via an expression of either SubClass or a further subclass. But it still doesn't have to be "the same" object.

这篇关于为什么“私人”可以方法可以从不同的实例访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆