如何向这个 Java 枚举函数解释结果?
鉴于
public enum Title {
MR("Mr."), MRS("Mrs."), MS("Ms.");
private final String title;
private Title(String t) {
title = t;
}
public String format(String last, String first) {
return title + "" + first + "" + last;
}
}
public static void main(String[] args){
System.out.println(Title.MR.format("Doe","John"));
}
有谁知道如何解释这一点?请记住,代码并不完全完整。这恰好是书上的一个问题。这个问题的答案是约翰·多伊先生。
谢谢
Given
public enum Title {
MR("Mr."), MRS("Mrs."), MS("Ms.");
private final String title;
private Title(String t) {
title = t;
}
public String format(String last, String first) {
return title + "" + first + "" + last;
}
}
public static void main(String[] args){
System.out.println(Title.MR.format("Doe","John"));
}
Does anyone know how to explain this ? Keep in mind the code is not fully complete. This happens to be a question from a book. The Answer to this question is Mr. John Doe.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,首先,考虑阅读 这个,以便了解什么是什么
Enum
、如何工作原理以及什么时候你应该使用它。现在,关于您的
Enum
示例,您正在声明一个具有三个可能值的Enum
类型:MR
、MRS
和MS
。Enum
就像Class
一样,可以有方法和构造函数。在您的示例中,Title
有一个参数构造函数 - 它存储Title
的描述 - 以及一个基本上将描述添加到给定名称前面的方法 -format
方法。因此,当您调用
Title.MR.format("Doe","John"))
时,首先您会获得MR
Title
的实例>,然后调用format
方法,该方法返回Mr.John Doe
。另请注意,每个
Title
只创建一个实例,因此多次调用Title.MR
将始终返回相同的对象。Well, first of all, consider reading this, in order to understand what is an
Enum
, how it works, and when you should use it.Now, concerning your
Enum
example, you are declaring anEnum
erated type with three possible values:MR
,MRS
, andMS
.Enum
s, just likeClass
es, can have methods and constructors. In your example,Title
has a single argument constructor -- which stores a description of theTitle
--, and a method that basically prepends the description to a given name -- theformat
method.So, when you invoke
Title.MR.format("Doe","John"))
, first you get an instance of theMR
Title
, and then you invoke theformat
method, which returnsMr.John Doe
.Also note that only one instance of each
Title
is created, so that invokingTitle.MR
several times will always return the same objet.这是将原始代码片段从
enum
重写为class
;它捕获了原始代码片段的大部分功能。我还使用String.format
用于教学目的。请注意与
enum
代码的相似性。但还要注意,这个版本更加冗长。重要的是,您必须了解足够的 Java 知识才能理解该版本为何如此工作。一旦完成,理解enum
版本就很简单:enum
是 Java 中的类。它不像 C/C++ 对应类型那样“简单”。参考文献
相关 问题
关于 Java 与 C++ 差异的
关于各种
enum
用法:另请参阅
int
常量EnumSet
代替位字段EnumMap
代替序数索引API 链接
java.lang.Enum
java.lang.EnumSet
java.util.EnumMap
< /a>This is a rewriting of the original snippet from an
enum
to aclass
; it captures most what the original snippet does. I also rewrote the string concatenation part usingString.format
for instructional purposes.Note the similarity with the
enum
code. But note also that this version is much more verbose. It's important that you know enough Java to understand why this version works the way it is. Once you do, understanding theenum
version is simple:enum
are classes in Java. It is not as "simple" a type as the C/C++ counterpart.References
Related questions
On Java vs C++ difference:
On various
enum
usage:See also
int
constantsEnumSet
instead of bit fieldsEnumMap
instead of ordinal indexingAPI links
java.lang.Enum
java.lang.EnumSet
java.util.EnumMap
假设问题是结果是如何伪造的,这里是带有一些注释的源代码:
希望能让代码更清晰。
Supposing the question is how the result is fabricated, here's the source code with some comments:
Hope that makes the code clearer.