How to explain the result to this Java enum function?
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 questi开发者_如何学Goon is Mr. John Doe.
Thanks
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 an Enum
erated type with three possible values: MR
, MRS
, and MS
. Enum
s, just like Class
es, can have methods and constructors. In your example, Title
has a single argument constructor -- which stores a description of the Title
--, and a method that basically prepends the description to a given name -- the format
method.
So, when you invoke Title.MR.format("Doe","John"))
, first you get an instance of the MR
Title
, and then you invoke the format
method, which returns Mr.John Doe
.
Also note that only one instance of each Title
is created, so that invoking Title.MR
several times will always return the same objet.
This is a rewriting of the original snippet from an enum
to a class
; it captures most what the original snippet does. I also rewrote the string concatenation part using String.format
for instructional purposes.
public class Title {
public static final Title MR = new Title("Mr.");
public static final Title MRS = new Title("Mrs.");
public static final Title MS = new Title("Ms.");
private final String title;
private Title(String t) { title = t; }
public String format(String last, String first) {
return String.format("%s %s %s", title, first, last);
}
public static void main(String[] args){
System.out.println(Title.MR.format("Doe","John"));
// prints "Mr. John Doe"
}
}
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 the enum
version is simple: enum
are classes in Java. It is not as "simple" a type as the C/C++ counterpart.
References
- Java Language Guide/Enums
- JLS 8.9 Enums
Related questions
On Java vs C++ difference:
- Difference of Enum between java and C++?
- Enum in Java. Advantages?
On various enum
usage:
- Java Enum Item Description
- Get enum by its inner field
- Can I add a function to enums in Java? (ABSOLUTELY!)
- Is there a a C-like way to get item number from enum in java ?
- (Yes, but why when there are so much better alternatives!?
See also
- Effective Java 2nd Edition, Chapter 6: Enums and Annotations
- Item 30: Use enums instead of
int
constants - Item 31: Use instance fields instead of ordinals
- Item 32: Use
EnumSet
instead of bit fields - Item 33: Use
EnumMap
instead of ordinal indexing
- Item 30: Use enums instead of
API 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:
public enum Title { // Declare an enum named Title
// Declare the enumerations with parameters that correspond with the constructor
MR("Mr."), MRS("Mrs."), MS("Ms.");
private final String title; // Declare String attribute (has null value at this point)
private Title(String t) { // enum constructor which accepts a String
title = t; // Initialize title String with parameter t
}
// format method which accepts last and first Strings and returns a String
public String format(String last, String first) {
// return is a String constructed from title first and last Strings.
return title + "" + first + "" + last;
}
}
// Main method which is triggered when the class is run
public static void main(String[] args){
// Output to console a call to the MR enum of Title with parameters Doe and John
System.out.println(Title.MR.format("Doe","John"));
}
Hope that makes the code clearer.
精彩评论