Java 本地类和接口
我想知道下一件事是否可以实现:
假设我有 2 个接口,而每个接口都有 1 个函数头。 例如,iterface1 有函数 g(...),interface2 有函数 f(...)
现在,我创建一个类并声明该类正在实现这两个接口。 在类中,我尝试做下一件事:
我开始实现函数 g(...) ,在它的实现中,我创建一个实现 interface2 的本地类,并向该类添加 f(...) 的实现。
I was wondering if the next thing is possible for implementation:
Lets say I've got 2 interfaces while each one of them has 1 function header.
For example, iterface1 has function g(...) and interface2 has function f(...)
Now, I make a class and declaring that this class is implementing these 2 interfaces.
In the class I try doing the next thing:
I start implementing function g(...) and in it's implementation I make a local class that implements interface2 and I add to this class the implementation of f(...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太确定你的意思。我正在想象这样的事情:
这就是你的意思吗?
在这种情况下,答案是否定的。内部类 (
InnerClass
) 工作正常,但它不算作外部类的f
实现。您仍然需要在MyClass
中实现f
:I'm not quite sure what you mean. I am picturing something like this:
Is that what you meant?
In this case, the answer is no. The inner class (
InnerClass
) works fine, but it doesn't count as an implementation off
for the outer class. You would still need to implementf
inMyClass
:是的,这是合法的。然而,类并不实现接口,因为内部类实现了接口。该类必须显式实现接口或将自身声明为抽象。
Yes it is legal. However a class does not implement an interface because an inner class implements it. The class must implement the interface explicitly or declare itself as abstract.
是的,这是合法的。在您给出的示例中,您的类应该实现两个接口的所有方法,并且您的本地类应该实现interface2 的所有方法。
Yes, it's legal. In the example you've given, your class should implement all methods of both interfaces, and your local class should implement all methods of interface2.