当尝试在位于其他包中的子类中使用protected构造函数时,Eclipse显示错误 [英] Eclipse shows an error when trying to use protected constructor in a subclass located in other package

查看:188
本文介绍了当尝试在位于其他包中的子类中使用protected构造函数时,Eclipse显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Stackoverflow社区: - ]

Hello Stackoverflow community :-]

我是一个新成员,我首先感谢您提供非常有用的建议和更正。因为我是法国人,如果我的英语不完美,请原谅我。

I am a new member and first I would like to thank you for the very helpful advice and correction that you provide. As I am french, please forgive me if my english is not perfect.

这里是我的问题:我目前学习Java编程语言,我想测试一些继承的东西。如果我理解正确,一个声明为 protected 的字段可以由与声明的protected字段在同一个包中的类访问,并且由它的所有子类,无论他们是否在同一个包。

Here is my question : I'm currently learning Java programming language, and I wanted to test some inheritance stuff. If I understood right, a field declared as protected can be accessed by classes which are in the same package as the class where protected field is declared, and by all of its subclasses, whether they are in the same package or not.

所以,我做了这4个类来测试这个。我有一个名为package1的包包含类A和C.我还有一个名为包2包含类A2和C,其中A2扩展A的第二个包。两个C类有完全相同的代码,只是包他们位于变化。它们不扩展A。

So, I did these 4 classes to test this. I have a package named "package1" containing classes A and C. I also have a second package named "package 2" containing classes A2 and C, where A2 extends A. The two C classes have exactly the same code, just the package where they are located changes.They do not extend A.

在类中,我声明了一些具有不同访问属性的成员,特别是用 protected 可见性。这里是四个类的代码。

In A class, I declared some members with different access properties, especially the constructor which is declared with protected visibility. Here is the code of the four classes.

package1,A类:

package1, class A :

package package1;

public class A {

    public int a;
    protected int b;
    private int c;
    int d;

    protected static int h = 30;

    protected void aff(){
        System.out.println(h);
    }

    protected A(){
        a = 1;
        b = 2;
        c = 3;
        d = 4;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

包1,类C:

package package1;

public class C {

    public C(){
        super();
    }

    public void app(){
        A obj = new A(); //////// OK
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        A obj = new A(); //////// OK
        obj.aff(); //////// OK

        System.out.println(obj.a);

    }

}

A2(扩展A):

package package2;
import package1.A;

public class A2 extends A{

    public int x;

    A2(){
        super();
    }


    public void app(){
        A obj = new A(); //////// ERROR
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        A obj = new A(); //////// ERROR

        A2 obj2 = new A2();
        obj2.aff(); //////// OK



    }

}

package2,class C:

package2, class C :

package package2;
import package1.A;

public class C {


    public C(){
        super();
    }

    public void app(){

        A obj = new A(); //////// ERROR
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        A obj = new A(); //////// ERROR
        obj.aff(); //////// ERROR

        System.out.println(obj.a);

    }

}

在package2中,代码 A obj = new A(); 抛出一个错误,但package1中的C类不是这样。这是正确的,因为构造函数声明为 protected 并且package2中的C不是A的子类,而C在package1中。

For C class in package2, the code A obj = new A(); throws an error but it's not the case for C class in package1. That's correct because constructor is declared as protected and C in package2 is not a subclass of A, while C is in package1. To that point, I understand.

我遇到的问题是代码 A obj = new A(); 在类A2中:无论它写在哪里,它抛出一个错误构造函数A()不可见 ...因为类A构造函数声明为 protected ,为什么不能在A2类中实例化A类型的对象?

Where I have a problem is with the code A obj = new A(); in class A2 : wherever it is written, it throws an error The constructor A() is not visible... As class A constructor is declared as protected, why couldn't I instantiate an object of type A in A2 class ?

当我声明一个构造函数为 public 时,它工作正常。此外,如果我把A2类放在package1通过让它的代码,它的工作。看起来如果一个构造函数被声明为 protected ,实例化A子类中的一个对象是可能的,如果子类位于同一个包中。

When I declare A constructor as public, it works fine. Besides, if I put A2 class in package1 by letting code as it is, it works too. It seems that instantiating A object in a subclass of A is only possible if the subclass is located in the same package if A constructor is declared as protected.

但是,正如你所看到的,如果我首先实例化一个A2对象,然后调用类A protected aff ()方法,它的工作和受保护的规则被尊重。

However, as you can see, if I first instantiate a A2 object and then call the class A protected aff() method, there it works and the protected rule is respected.

有人有这个错误的解释吗?当实例化它的子类中的超类对象时,如果超类构造函数声明为 protected ,那么这个子类总是位于与其超类相同的包中?为什么会是这样的情况?

Does someone have the explanation for this error ? When instantiating an object of superclass in its subclass, does this subclass always be located in the same package as its superclass, if superclass constructor is declared as protected ? And why is it the case if so ?

或者这是否必须处理一个构造函数不被子类继承的事实?但我不知道为什么是这样的情况...

Or does this has to deal with the fact that a constructor is not inherited by subclasses ? But I can't figure out to see why if it's the case...

提前感谢你花费时间阅读和回答: - ]

Thanks a lot in advance for taking time to read and answer :-]

推荐答案

这很有趣,所以让我尝试总结一下。请参阅 JLS#6.6.1

This is fun so let me try to summarize it. see JLS#6.6.1

protected 可以限定构造函数或类成员。

protected can qualify a constructor or a member of a class.

member包括字段/方法(静态/实例),嵌套类/接口(静态/内部)

"member" includes field/method (static/instance), nested class/interface (static/inner)

class A {
    protected int f
    protected void m(){}
    protected class X{}
    protected interface Y{}






首先,要访问受保护的构造函数/成员, c $ c> A )必须可访问。假设是这样,那么 -


First, to access a protected constructor/member, the enclosing class (e.g. A) must be accessible. Assume that's the case, then --

- 在程序包内 -

--Inside the package --

受保护的构造函数或成员可在同一包中的任何位置访问。

A protected constructor or member is accessible anywhere within the same package.

- 包外 -

受保护的构造函数只能在子类构造函数中访问, super()调用,或作为匿名类实例化。

A protected constructor is only accessible within subclass constructors, either as a super() call, or as an anonymous class instantiation.

受保护的静态字段/方法,嵌套类/接口可在子类体内的任何位置访问。

A protected static field/method, nested class/interface is accessible anywhere within subclass bodies.

受保护的实例字段/方法更复杂 -

A protected instance field/method is more complex --


  • A类

  • obj.m

  • < obj 的类型为C
  • protected "m" is defined in a class A
  • obj.m is accessed in class B (outside A's package)
  • obj 's type is C

访问 obj.m 仅在B是A的子类,C是B的子类或C是B时才被授予。

The access obj.m is granted only if B is subclass of A, and C is subclass of B or C is B.

super.m 始终允许;然而,它不清楚如何JLS框架的问题。看来,访问应该被视为与 this.m 相同,因此允许访问。

super.m is always allowed; however, it's unclear how JLS frames the issue. It seems that the access should be treated the same as this.m, therefore access is allowed.

这篇关于当尝试在位于其他包中的子类中使用protected构造函数时,Eclipse显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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