我们为什么要在类中声明一个接口?

时间:2022-09-11 12:10:46

Why should we declare an interface inside a class in Java?

我们为什么要在Java中声明一个类中的接口?

For example:

public class GenericModelLinker implements IModelLinker {

  private static final Logger LOG =LoggerFactory.getLogger(GenericModelLinker.class);
  private String joinAsPropertyField;
  private boolean joinAsListEntry;
  private boolean clearList;
  private List<Link> joins;

  //instead of a scalar property
  private String uniqueProperty;

  public interface Link {

    Object getProperty(IAdaptable n);

    void setProperty(IAdaptable n, Object value);

  }
}

5 个解决方案

#1


26  

When you want to gather some fields in an object in order to emphasize a concept, you could either create an external class, or an internal (called either nested (static ones) or inner).

当您想要收集对象中的某些字段以强调概念时,您可以创建外部类,也可以创建内部(称为嵌套(静态)或内部)。

If you want to emphasize the fact that this cooperative class makes strictly no sense (has no use) outside the original object use, you could make it nested/inner.

如果你想强调这个合作类在原始对象使用之外完全没有意义(没有用)的事实,你可以使它嵌套/内部。

Thus, when dealing with some hierarchy, you can describe a "nested" interface, which will be implemented by the wrapping class's subclasses.

因此,在处理某些层次结构时,您可以描述一个“嵌套”接口,该接口将由包装类的子类实现。

In the JDK, the most significant example would be Map.Entry inner interface, defined within Map interface and implemented by various ways by HashMap, LinkedHashMap etc...

在JDK中,最重要的例子是Map.Entry内部接口,在Map接口中定义并通过HashMap,LinkedHashMap等各种方式实现...

And of course, Map.Entry needed to be declared as public in order to be accessible while iterating the map wherever the code is.

当然,Map.Entry需要声明为public,以便在代码所在的任何地方迭代地图时都可以访问。

#2


11  

If the interface definition is small and the interface will only be used by clients of the class it's defined in, it's a good way to organize the code. Otherwise, the interface should be defined in its own file.

如果接口定义很小并且接口仅由其定义的类的客户端使用,则这是组织代码的好方法。否则,接口应在其自己的文件中定义。

#3


2  

This is inner interface. Java programming language allows defining inner classes and interfaces. This is typically useful if you want to limit visibility of this class or interface by scope of current outer class.

这是内部接口。 Java编程语言允许定义内部类和接口。如果要按当前外部类的范围限制此类或接口的可见性,这通常很有用。

Some people use this mechanism for creating a kind of namespace. IMHO this is abuse of the language feature (in most cases).

有些人使用这种机制来创建一种命名空间。恕我直言,这是滥用语言功能(在大多数情况下)。

#4


1  

Inside your class you may need multiple implementations of an interface, which is only relevant to this particular class. In that case make it an inner interface, rather than a public / package-private one.
Only an interface inside a class can be declared private or protected. Sometimes, that makes sense, when the interface is only appropriate for use inside the outer class (or its subclasses).

在您的类中,您可能需要多个接口实现,这仅与此特定类相关。在这种情况下,使其成为内部接口,而不是公共/包私有。只有类中的接口可以声明为私有或受保护。有时,当接口仅适合在外部类(或其子类)中使用时,这是有道理的。

#5


1  

To encapsulate behavior in a generic and resuable way.

以通用和可恢复的方式封装行为。

Apart from nice example of Map.Entry used by Map implementation classes another good example is implementation of Strategy Pattern, where a execution strategy is evaluated and applied internally.

除了Map实现类使用的Map.Entry的好例子之外,另一个很好的例子是策略模式的实现,其中执行策略在内部进行评估和应用。

class Test
{

..
interface Cipher {
 doAction();
}
class RAMPCipher implements Cipher{}
class DiskCipher implements Cipher{}
..
}

#1


26  

When you want to gather some fields in an object in order to emphasize a concept, you could either create an external class, or an internal (called either nested (static ones) or inner).

当您想要收集对象中的某些字段以强调概念时,您可以创建外部类,也可以创建内部(称为嵌套(静态)或内部)。

If you want to emphasize the fact that this cooperative class makes strictly no sense (has no use) outside the original object use, you could make it nested/inner.

如果你想强调这个合作类在原始对象使用之外完全没有意义(没有用)的事实,你可以使它嵌套/内部。

Thus, when dealing with some hierarchy, you can describe a "nested" interface, which will be implemented by the wrapping class's subclasses.

因此,在处理某些层次结构时,您可以描述一个“嵌套”接口,该接口将由包装类的子类实现。

In the JDK, the most significant example would be Map.Entry inner interface, defined within Map interface and implemented by various ways by HashMap, LinkedHashMap etc...

在JDK中,最重要的例子是Map.Entry内部接口,在Map接口中定义并通过HashMap,LinkedHashMap等各种方式实现...

And of course, Map.Entry needed to be declared as public in order to be accessible while iterating the map wherever the code is.

当然,Map.Entry需要声明为public,以便在代码所在的任何地方迭代地图时都可以访问。

#2


11  

If the interface definition is small and the interface will only be used by clients of the class it's defined in, it's a good way to organize the code. Otherwise, the interface should be defined in its own file.

如果接口定义很小并且接口仅由其定义的类的客户端使用,则这是组织代码的好方法。否则,接口应在其自己的文件中定义。

#3


2  

This is inner interface. Java programming language allows defining inner classes and interfaces. This is typically useful if you want to limit visibility of this class or interface by scope of current outer class.

这是内部接口。 Java编程语言允许定义内部类和接口。如果要按当前外部类的范围限制此类或接口的可见性,这通常很有用。

Some people use this mechanism for creating a kind of namespace. IMHO this is abuse of the language feature (in most cases).

有些人使用这种机制来创建一种命名空间。恕我直言,这是滥用语言功能(在大多数情况下)。

#4


1  

Inside your class you may need multiple implementations of an interface, which is only relevant to this particular class. In that case make it an inner interface, rather than a public / package-private one.
Only an interface inside a class can be declared private or protected. Sometimes, that makes sense, when the interface is only appropriate for use inside the outer class (or its subclasses).

在您的类中,您可能需要多个接口实现,这仅与此特定类相关。在这种情况下,使其成为内部接口,而不是公共/包私有。只有类中的接口可以声明为私有或受保护。有时,当接口仅适合在外部类(或其子类)中使用时,这是有道理的。

#5


1  

To encapsulate behavior in a generic and resuable way.

以通用和可恢复的方式封装行为。

Apart from nice example of Map.Entry used by Map implementation classes another good example is implementation of Strategy Pattern, where a execution strategy is evaluated and applied internally.

除了Map实现类使用的Map.Entry的好例子之外,另一个很好的例子是策略模式的实现,其中执行策略在内部进行评估和应用。

class Test
{

..
interface Cipher {
 doAction();
}
class RAMPCipher implements Cipher{}
class DiskCipher implements Cipher{}
..
}