java中的访问问题

发布于 2024-08-14 01:05:30 字数 483 浏览 2 评论 0原文

我正在使用第 3 方框架,结果我需要将其一些对象包装为我的一个类的委托。

class Foo { // 3rd party class.
    protected void method() {}
}

class FooWrapper extends Foo {
    private Foo mDelegate;

    public FooWrapper(Foo inDelegate) {
        mDelegate = inDelegate;
    }

    protected void method() {
        mDelegate.method(); // error can't access protected method() of mDelegate
    }
}

所以问题就来了。我需要将此方法委托给内部对象,但它受保护,因此无法访问。

关于解决这个特定问题的方法有什么想法吗?这是针对 Java 1.3 的。

I am working with a 3rd party framework and it turns out I need to wrap some of its objects as a delegate to one of my classes.

class Foo { // 3rd party class.
    protected void method() {}
}

class FooWrapper extends Foo {
    private Foo mDelegate;

    public FooWrapper(Foo inDelegate) {
        mDelegate = inDelegate;
    }

    protected void method() {
        mDelegate.method(); // error can't access protected method() of mDelegate
    }
}

So there is the problem. I need to delegate this method to the internal object but its protected and therefore not accessible.

Any ideas on ways to solve this particular problem? This is for Java 1.3.

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

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

发布评论

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

评论(4

拿命拼未来 2024-08-21 01:05:30

为什么要构建一个单独的 Foo 实例? FooWrapper 已经是一个 Foo。

class Foo {
    protected void method() {}
}

class FooWrapper extends Foo {

    protected void method() {
        super.method();
    }
}

编辑:如果您确实必须有单独的实例,一种方法(尽管有点难看)是使用反射:

public class Foo {

    protected void method() {
        System.out.println("In Foo.method()");
    }
}

public class FooWrapper extends Foo {

    private Foo foo;

    public FooWrapper(Foo foo) {
        this.foo = foo;
    }

    public void method() {
        try {
            Method m = foo.getClass().getDeclaredMethod("method", null);
            m.setAccessible(true);
            m.invoke(foo, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

编辑2:根据下面的评论,有两个子类Foo 的一个版本,它只提供受保护方法的公共版本,并且重写了所有重要的方法。第一个的外观和行为与 Foo 完全相同,第二个可以做任何你需要做的事情:

public class Foo {

    protected void method() {
        System.out.println("In Foo.method()");
    }
}

public class DelegateFoo extends Foo {

    public void method() {
        super.method();
    }
}

public class FooWrapper extends Foo {

    private DelegateFoo foo;

    public FooWrapper(DelegateFoo foo) {
        this.foo = foo;
    }

    public void method() {
        foo.method();
        /* extra logic here */
    }
}

Why are you constructing a separate instance of Foo? FooWrapper is already a Foo.

class Foo {
    protected void method() {}
}

class FooWrapper extends Foo {

    protected void method() {
        super.method();
    }
}

Edit: if you really have to have separate instance, one way (albeit slightly ugly) is to use reflection:

public class Foo {

    protected void method() {
        System.out.println("In Foo.method()");
    }
}

public class FooWrapper extends Foo {

    private Foo foo;

    public FooWrapper(Foo foo) {
        this.foo = foo;
    }

    public void method() {
        try {
            Method m = foo.getClass().getDeclaredMethod("method", null);
            m.setAccessible(true);
            m.invoke(foo, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Edit2: Based on your comment below, have two sub-classes of Foo, one that simply provides public versions of the protected methods, and one that has overrides all of the important methods. The first will look and act exactly like a Foo, the second can do whatever you need doing:

public class Foo {

    protected void method() {
        System.out.println("In Foo.method()");
    }
}

public class DelegateFoo extends Foo {

    public void method() {
        super.method();
    }
}

public class FooWrapper extends Foo {

    private DelegateFoo foo;

    public FooWrapper(DelegateFoo foo) {
        this.foo = foo;
    }

    public void method() {
        foo.method();
        /* extra logic here */
    }
}
卷耳 2024-08-21 01:05:30

当我遇到这种情况时,我所做的就是在与 Foo 相同的包中创建一个 PublicFoo (但在您的源代码中),让它扩展 Foo ,并覆盖受保护的方法并将其公开。

现在,FooWrapper 可以扩展 PublicFoo 并做任何你想做的事情。

但请注意,如果原始 jar 已签名并密封,则需要先删除签名才能部署它。 (您可以进入jar中的Manifest目录,然后删除证书即可)

What I've done when I've been in a situation like this, and its not nice, but it works, is create a PublicFoo in the same package as Foo (but in your source), have it extend Foo, and override the protected method and make it public.

Now, FooWrapper can extend PublicFoo and do whatever you want.

Note, however, if the original jar is signed and sealed, youll need to remove the signatures before you can deploy it. (You can go into the Manifest directory in the jar, and simply delete the certificates)

温柔女人霸气范 2024-08-21 01:05:30

好吧,如果它可以公开访问其属性(无论是 getter 还是公共变量),您可以创建一个复制构造函数:

public FooWrapper(Foo inDelegate) {
    this.property1 = inDelegate.getProperty1();
    this.property2 = inDelegate.getProperty2();
    // for every property
}

如果属性是对象...很好,甚至更好,因为这样您所做的任何更改都会在访问时显示出来也通过原始对象! (也就是说,直到您替换其中一个对象,对于像 String 这样的不可变类,您必须替换这些对象。)

Well, if it has public access to its properties (be they getters or public variables), you could create a copy constructor:

public FooWrapper(Foo inDelegate) {
    this.property1 = inDelegate.getProperty1();
    this.property2 = inDelegate.getProperty2();
    // for every property
}

If the properties are objects... great, even better, because then any changes you make will show up when accessed through the original object, too! (That is, until you replace one of those objects, which you have to for immutable classes like String.)

秋凉 2024-08-21 01:05:30

子类应该有权访问超类的受保护方法。

如果你只调用 super.method() 而不是 mDelegate.method(),它就会消失;您都在包装父类并实例化它。

A subclass should have access to a superclass's protected methods.

If you just call super.method() instead of mDelegate.method(), it'll go; you're both wrapping the parent class and instantiating it.

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