在Java中用接口定义类的能力的实用方面? [英] Practical side of the ability to define a class within an interface in Java?
本文介绍了在Java中用接口定义类的能力的实用方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中用接口定义类的能力的实际方面是什么:
What would be the practical side of the ability to define a class within an interface in Java:
interface IFoo
{
class Bar
{
void foobar ()
{
System.out.println("foobaring...");
}
}
}
推荐答案
我可以想到另一种用法,而不是Eric P链接的用法:定义接口的默认/无操作实现。
I can think of another usage than those linked by Eric P: defining a default/no-op implementation of the interface.
./ alex
interface IEmployee
{
void workHard ();
void procrastinate ();
class DefaultEmployee implements IEmployee
{
void workHard () { procrastinate(); };
void procrastinate () {};
}
}
又一个样本 - 实施< a href =http://en.wikipedia.org/wiki/Null_Object_pattern =nofollow noreferrer>空对象模式:
Yet another sample — implementation of Null Object Pattern:
interface IFoo
{
void doFoo();
IFoo NULL_FOO = new NullFoo();
final class NullFoo implements IFoo
{
public void doFoo () {};
private NullFoo () {};
}
}
...
IFoo foo = IFoo.NULL_FOO;
...
bar.addFooListener (foo);
...
这篇关于在Java中用接口定义类的能力的实用方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文