Java-无法访问嵌套类扩展的超类的保护方法

发布于 2025-02-07 10:20:01 字数 714 浏览 3 评论 0 原文

我是Java的新手,并掌握了受保护的可见性。

我有Altera A的A类方法1,如下所示 -

public abstract class A { 
    public A(...){}

    protected void method1(){
        // do something
    }

现在我有另一个B级B级。B类具有嵌套的静态类,D级扩展A类A级

public class B extends C {
    public B(...){
        super(...);
    }

    private static class D extends A {
        D(...){super(...);}
    }

    public void method2() {
        D record = new D();
        record.method1(); // need to access the protected method1 of class A, which is the superclass of nested class D. Getting the error here
    }
}

I i Carter i Carter i Cerfor i Cerfor this Orror -Method1在A类中保护了访问 现在我的问题是如何访问方法1?我无法将其更改为A类公开

I'm new to java and getting the hang of protected visibility.

I have Class A which has protected method method1 as follows -

public abstract class A { 
    public A(...){}

    protected void method1(){
        // do something
    }

Now I have another Class B which extends a different Class C. Class B has a nested static class, Class D extending Class A

public class B extends C {
    public B(...){
        super(...);
    }

    private static class D extends A {
        D(...){super(...);}
    }

    public void method2() {
        D record = new D();
        record.method1(); // need to access the protected method1 of class A, which is the superclass of nested class D. Getting the error here
    }
}

I get this error - method1 has protected access in Class A
Now my question is how can I access method1? I cannot change it to public in class A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2025-02-14 10:20:01

您需要在D类中创建代理方法,如下所述。 D类可以访问A类的受保护方法,但是D类的实例无法直接调用受保护的方法,并且必须具有代理方法来调用超级类的受保护方法。

在D类中,您需要为A类中的方法创建一个公共Getter,因此:

public class A {
    protected void protectedMethod() {}
}

public class D extends A {
    public void callProtectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();

record.callProtectedMethod();

You need to create a proxy method in class D as outlined below. Class D can access Class A's protected methods, but an instance of Class D can't call protected methods directly, and must have a proxy method to call the protected method of the super class.

In class D, you need to create a public getter for the method in Class A, so:

public class A {
    protected void protectedMethod() {}
}

public class D extends A {
    public void callProtectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();

record.callProtectedMethod();
倾听心声的旋律 2025-02-14 10:20:01

由于A.Method1()受到保护,因此只能从

A. 的同一类/相同的包装/子女访问A. A

解决方案将包装器用作@Alex Ander或更改A.Method1()的更改修饰符,向public

“

Since A.method1() is protected, it is only accessible from same class/same package/child of A.

In your code, your are calling A.method1() in B, that is not accessible if B not in same package as A

The solution is using a wrapper as @Alex Ander or change modifier of A.method1() to public

Java Access Modifiers

物价感观 2025-02-14 10:20:01

问题是由受保护的 method1() Method2()属于B的B和D之外的B,B不在同一软件包中的B。使用或

D。 ,其继承类或来自同一软件包。

这就是为什么当在D内部创建包装器方法时,问题解决了,因为受保护的 Method1()是从内部调用的。

我们仍然可以覆盖受保护的 method1() A并在必要时通过D公开。 (从上面的包装器示例修改)

class A {
    protected void protectedMethod() {}
}

class D extends A {
    @Override
    public void protectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();
record.protectedMethod();

The problem was caused by the protected method1() called from method2(), which belongs to B, which is outside A and D, and B is not in the same package with A or D.

The method1() should only used/called inside A or D. That's what protected is used for, to ensure it is used only in the owner class, its inheriting classes or from the same package.

That's why when the wrapper method created inside D, the problem solved, since the protected method1() is called from inside D.

We still can override the protected method1() of A and make it public through D if necessary. (Modified from the wrapper example above)

class A {
    protected void protectedMethod() {}
}

class D extends A {
    @Override
    public void protectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();
record.protectedMethod();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文