在 Java 的接口(interface)中定义类的能力的实际方面是什么:
interface IFoo
{
class Bar
{
void foobar ()
{
System.out.println("foobaring...");
}
}
}
最佳答案
除了 Eric P 所链接的用法之外,我还能想到另一种用法:定义接口(interface)的默认/无操作实现。
./亚历克斯
interface IEmployee
{
void workHard ();
void procrastinate ();
class DefaultEmployee implements IEmployee
{
void workHard () { procrastinate(); };
void procrastinate () {};
}
}
又一个示例——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 - 在 Java 的接口(interface)中定义类的能力的实际方面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/594789/