如何向这个 Java 枚举函数解释结果?

发布于 2024-09-09 05:52:22 字数 456 浏览 1 评论 0原文

鉴于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢好甜 2024-09-16 05:52:22

好吧,首先,考虑阅读 这个,以便了解什么是什么Enum如何工作原理以及什么时候你应该使用它。

现在,关于您的 Enum 示例,您正在声明一个具有三个可能值的 Enum 类型:MRMRSMSEnum 就像 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 an Enumerated type with three possible values: MR, MRS, and MS. Enums, just like Classes, 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.

毁梦 2024-09-16 05:52:22

这是将原始代码片段从 enum 重写为 class;它捕获了原始代码片段的大部分功能。我还使用 String.format 用于教学目的。

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"
    }
}

请注意与 enum 代码的相似性。但还要注意,这个版本更加冗长。重要的是,您必须了解足够的 Java 知识才能理解该版本为何如此工作。一旦完成,理解 enum 版本就很简单:enum 是 Java 中的类。它不像 C/C++ 对应类型那样“简单”。

参考文献

相关 问题

关于 Java 与 C++ 差异的

关于各种enum用法:

另请参阅

  • 《Effective Java 第二版》,第 6 章:枚举和注释
    • 第 30 项:使用枚举代替 int 常量
    • 第 31 项:使用实例字段代替序数
    • 第 32 项:使用 EnumSet 代替位字段
    • 第 33 项:使用 EnumMap 代替序数索引

API 链接

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

Related questions

On Java vs C++ difference:

On various enum usage:

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

API links

酒绊 2024-09-16 05:52:22

假设问题是结果是如何伪造的,这里是带有一些注释的源代码:

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"));
}

希望能让代码更清晰。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文