我有一个接口(interface),声明如下:
/**
* @param T - the type of entity.
* @param C - the type of entity container will be returned.
*/
public interface FindByNamedQuery<T extends Serializable, C extends Collection<T>> extends Command {
C executeNamedQuery(String namedQuery);
}
我想知道我是否可以(应该)打破 Java 命名约定来做到这一点:
public interface FindByNamedQuery<ENTITY_TYPE extends Serializable, RETURNED_CONTAINER extends Collection<ENTITY_TYPE>> extends Command {
RETURNED_CONTAINER executeNamedQuery(String namedQuery);
}
最佳答案
自从 1990 年代中期开始使用单字符约定后,我开始不同意它。
我发现可读的名字更易读。这有助于理解泛型类型的实现和接口(interface)。
对于 Java,歧义问题似乎被夸大了。很少有类名是全大写的。常量不在与类名相同的上下文中使用。
@param JavaDoc 元素确实可以提供更长的描述。但是,JavaDocs 不一定是可见的,这也是事实。 (例如,Eclipse 中有一个显示类型参数名称的内容辅助。)
例如,比较:
public final class EventProducer<L extends IEventListener<E>,E>
implements IEventProducer<L,E> {
到:
public final class EventProducer<LISTENER extends IEventListener<EVENT>,EVENT>
implements IEventProducer<LISTENER, EVENT> {
尽管 Sun/Oracle 建议将单字符名称作为约定,但可以更改约定。挑战这一公约的后果是轻微的。如果您和您的团队更喜欢为您的类型参数命名有意义的名称,我个人会这样做。
编辑(2015 年)
Google style for Java允许单字母名称和以 T 结尾的多字符类名称。
Each type variable is named in one of two styles:
A single capital letter, optionally followed by a single numeral (such as E, T, X, T2)
A name in the form used for classes (see Section 5.2.2, Class names), followed by the capital letter T (examples: RequestT, FooBarT).
关于java - 打破java泛型命名约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6143402/