Java的通用类型参数命名约定(具有多个字符)? [英] Generic type parameter naming convention for Java (with multiple chars)?

查看:15
本文介绍了Java的通用类型参数命名约定(具有多个字符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我编写的某些接口中,我想用多个字符命名泛型类型参数,以使代码更具可读性.

In some interfaces I wrote I'd like to name generic type parameters with more than one character to make the code more readable.

有点像......

Map<Key,Value>

而不是这个...

Map<K,V>

但是说到方法,类型参数看起来像java-classes,这也很混乱.

But when it comes to methods, the type-parameters look like java-classes which is also confusing.

public void put(Key key, Value value)

这似乎 Key 和 Value 是类.我发现或想到了一些符号,但没有像 Sun 的约定或一般最佳实践那样.

This seems like Key and Value are classes. I found or thought of some notations, but nothing like a convention from Sun or a general best-practice.

我猜到或找到的替代方案...

Alternatives I guessed or found...

Map<KEY,VALUE>
Map<TKey,TValue>

推荐答案

Oracle 在 Java 教程 >泛型 >通用类型:

按照惯例,类型参数名称是单个大写字母.这与变量命名约定形成鲜明对比.你已经知道了,并且有充分的理由:如果没有这个约定,就很难区分类型变量和普通的类或接口名称.

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

最常用的类型参数名称是:

The most commonly used type parameter names are:

  • E - 元素(被 Java 集合框架广泛使用)
  • K - 键
  • N - 数字
  • T - 类型
  • V - 价值
  • S、U、V 等 - 第 2、第 3、第 4 种

您将在 Java SE API 和本课程的其余部分中看到这些名称的使用.

You'll see these names used throughout the Java SE API and the rest of this lesson.

我会坚持这样做,以避免开发人员和可能的维护人员之间产生混淆.

I'd stick to it to avoid the confusion among the developers and possible maintainers.

这篇关于Java的通用类型参数命名约定(具有多个字符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆