使用惯用语“提供默认接口实现”时无法覆盖方法并且无法访问字段
这是代码:
IDefaultInterface.aj:
public interface IDefaultInterface {
public void m1();
static aspect Impl{
public int f1;
public void IDefaultInterface.m1(){
}
}
}
DefaultInterfaceClass.java:
public class DefaultInterfaceClass implements IDefaultInterface {
@Override
public void m1() {
}
void mm() {
f1 = 9;
}
}
在第二段代码中,我尝试重写 m1() 方法并访问 f1 字段。编译器不允许两者之一。
如何克服这些限制?
额外的想法。如果在“AspectJ in action”第 2 版中没有提到使用这个习惯用法,那么效果应该与“扩展两者的默认实现(如果 Java 中允许多重继承)”相同,我不会太好奇。我相信对于大多数人来说,多重继承与 C++ 相关。那么,为什么不提供人们习惯的语义呢?
Here is code:
IDefaultInterface.aj:
public interface IDefaultInterface {
public void m1();
static aspect Impl{
public int f1;
public void IDefaultInterface.m1(){
}
}
}
DefaulstInterfaceClass.java:
public class DefaultInterfaceClass implements IDefaultInterface {
@Override
public void m1() {
}
void mm() {
f1 = 9;
}
}
In the second piece of code I'm trying to override m1() method and access f1 field. The compiler allows neither one.
How to overcome these limitations?
Additional thoughts. I would not wonder so much if in "AspectJ in action" 2 edition wasn't said about using this idiom that effect should be the same "as extending the default implementation for both (if multiple inheritance was allowed in Java)." I believe that multiple inheritance associated with C++ for majority. So, why not provide the semantics to which people used to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 AspectJ 不太熟悉,但我看到了一些有问题的事情:您的方面试图在接口中定义非抽象方法,并且您的类试图访问字段 f1 ,就好像它拥有该字段一样,当您已经在这方面宣布了f1。我不太确定你想在这里做什么,但我认为你没有以正确的方式去做。
I'm not fluent in AspectJ, but I see a couple of questionable things: your aspect is trying to define a non-abstract method in an interface, and your class is trying to access field f1 as if it owns the field, when you've declared f1 on the aspect. I'm not quite sure what you're trying to do here, but I don't think you're going about it in the right way.
首先,我拼错了 f1 声明。应该是
它解决了访问字段问题。
第二个问题通过使用以下代码解决:
然后:
First of all I misspelled f1 declaration. It should be
It solves access field problem.
The second problem is solved by using following code:
And then: