java中的访问问题
我正在使用第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要构建一个单独的 Foo 实例? FooWrapper 已经是一个 Foo。
编辑:如果您确实必须有单独的实例,一种方法(尽管有点难看)是使用反射:
编辑2:根据下面的评论,有两个子类Foo 的一个版本,它只提供受保护方法的公共版本,并且重写了所有重要的方法。第一个的外观和行为与 Foo 完全相同,第二个可以做任何你需要做的事情:
Why are you constructing a separate instance of Foo? FooWrapper is already a Foo.
Edit: if you really have to have separate instance, one way (albeit slightly ugly) is to use reflection:
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:
当我遇到这种情况时,我所做的就是在与 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)
好吧,如果它可以公开访问其属性(无论是 getter 还是公共变量),您可以创建一个复制构造函数:
如果属性是对象...很好,甚至更好,因为这样您所做的任何更改都会在访问时显示出来也通过原始对象! (也就是说,直到您替换其中一个对象,对于像 String 这样的不可变类,您必须替换这些对象。)
Well, if it has public access to its properties (be they getters or public variables), you could create a copy constructor:
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.)
子类应该有权访问超类的受保护方法。
如果你只调用 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.