为什么在同一个包中看不到包保护方法? [英] Why is package-protected method not visible in the same package?

查看:121
本文介绍了为什么在同一个包中看不到包保护方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个包 p1 p2 和类 p1.M1 p2.M12 扩展如下:

Assume we have two packages p1 and p2 and classes p1.M1 extended by p2.M12 as follows:

package p1;

public class M1 {
    void method1() {
        System.out.println("Method 1 called");
    }
}


package p2;

import p1.M1;

public class M12 extends M1 {
    void method2() {
        System.out.println("Method 2 called");
    }
}

让我们扩展 M12 p2.B

package p2;

public class B extends M12 {

    public void doSomething()  {
        method1();
        method2();
    }
} 

这会产生编译错误 method1 ,在 p1 内受到包保护,在 p2 中不可见。 method2 可见而没有问题。

This gives a compilation error as method1, being package-protected within p1 is not visible in p2. method2 is visible without problems.

现在让我们扩展 p2.M12 with p1.A

Now let's extend p2.M12 with p1.A:

package p1;

import p2.M12;

public class A extends M12 {

    public void doSomething() {
        method1();
        method2();
    }
}

这里我收到两个编译错误 method2()(这是可以理解的) method1()
< a href =https://i.stack.imgur.com/4P1Jv.png =noreferrer>

Here I'm getting a compilation error for both method2() (which is understandable) and method1():

我的问题是:为什么 method1 包中的包保护 p1 在同一个包 A 中不可见<$ c c $ c> p1 ?

My question is: why is the method1 which is package-protected in the package p1 is not visible in the class A from the same package p1?

推荐答案

首先,什么是班级成员? Java语言规范 states

First, what is a member of a class? The Java Language Specification states


类主体可能包含类成员的声明,
是,fields(第8.3节),方法(§8.4),类(§8.5)和接口
(§8.5)。

A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5).

它们是什么由...组成的? JLS声明

And what are they composed of? The JLS states


类类型的成员都是以下所有成员:

The members of a class type are all of the following:


  • 会员直接超类继承 (第8.1.4节),但类Object中没有直接超类

  • 从任何直接超接口继承的成员(第8.1.5节)

  • 在类的主体中声明的成员 (§8.1.6)

  • Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass
  • Members inherited from any direct superinterfaces (§8.1.5)
  • Members declared in the body of the class (§8.1.6)

它还提到


只有声明 protected public 的类的成员才是由声明的子类继承的
声明该类的
之外的包。

Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

所有这些都在继承章节


一个类 C 继承自其直接超类所有具体方法 m
(静态和实例)所有
以下的超类都是真的:

A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true:


  • m C 的直接超类的成员。

  • m public protected 使用与C`相同的包中的包访问权声明

  • C 中声明的方法没有签名,该签名是<$ c $签名的子签名(§8.4.2) c> m 。

  • m is a member of the direct superclass of C.
  • m is public, protected, or declared with package access in the same package as C`.
  • No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.

班级成员 M1 method1 (以及 Object 的所有方法)。 M12 ,与其直接超类不同的包 M1 ,不会继承 method1 M12 的成员因此仅 method2

The members of class M1 are method1 (and all the methods of Object). M12, being in a different package than its direct superclass, M1, does not inherit method1. The members of M12 are therefore only method2.

B 的直接超类是 M12 ,并且在同一个包中。因此它继承了它的成员 method2 B method1 一无所知。如果您使用 javac 编译了代码,则会收到找不到符号编译错误。 (似乎Eclipse试图猜测你想要做什么。)

The direct superclass of B is M12 and is in the same package. It therefore inherits its member, method2. B knows nothing about method1. If you had compiled your code with javac, you would have received a cannot find symbol compilation error instead. (It seems Eclipse is trying to guess what you were trying to do.)

同样, A的直接超类 M12 ,但是在不同的包中。由于这个原因,它不会继承 method2 A method1 method2 一无所知它没有继承它们。这两个都是无法找到的符号。

Similarly, the direct superclass of A is M12, but is in a different package. It does not inherit method2 for that reason. A doesn't know anything about method1 or method2 because it didn't inherit them. Both of those are symbols that cannot be found.

这篇关于为什么在同一个包中看不到包保护方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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