Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向这个Java枚举函数解释结果?_Java_Enums - Fatal编程技术网

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

如何向这个Java枚举函数解释结果?,java,enums,Java,Enums,给定的 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

给定的

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

有人知道怎么解释吗?请记住,代码并不是完全完整的。这恰好是一本书中的一个问题。这个问题的答案是约翰·多伊先生


P.>谢谢

< P> >首先,考虑阅读,以便理解<强> < <强> >是什么,<强> > <>强> > <>强> < /强>你应该使用它。

现在,关于您的
Enum
示例,您正在声明一个带有三个可能值的
Enum
rated类型:
MR
MRS
MS
Enum
s就像
Class
es一样,可以有方法构造函数。在您的示例中,
Title
有一个单参数构造函数——它存储
Title
的描述——和一个基本上将描述前置到给定名称的方法——
format
方法

因此,当您调用
Title.MR.format(“Doe”、“John”)
时,首先您会得到
MR
Title
的一个实例,然后调用
format
方法,该方法返回
MR.John Doe


还注意到,每个代码< >标题>代码>只有一个实例被创建,所以调用<代码>标题>先生<代码>几次将总是返回同一个对象。

嗯,首先,考虑阅读,以便理解<强>什么< /强>是一个,<强> > < <强> >,< <强> >你应该使用它。

现在,关于您的
Enum
示例,您正在声明一个带有三个可能值的
Enum
rated类型:
MR
MRS
MS
Enum
s就像
Class
es一样,可以有方法构造函数。在您的示例中,
Title
有一个单参数构造函数——它存储
Title
的描述——和一个基本上将描述前置到给定名称的方法——
format
方法

因此,当您调用
Title.MR.format(“Doe”、“John”)
时,首先您会得到
MR
Title
的一个实例,然后调用
format
方法,该方法返回
MR.John Doe


还请注意,每个
Title
只创建了一个实例,因此多次调用
Title.MR
将始终返回同一个对象。

假设问题是如何生成结果,下面是源代码和一些注释:

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

希望这能让代码更清晰。

假设问题是结果是如何产生的,下面是源代码和一些注释:

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

希望这能使代码更清晰。

这是将原始代码段从
枚举重写为
类;它捕获了原始代码段的大部分功能。出于教学目的,我还重写了字符串连接部分

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++对应类型那样“简单”

工具书类
相关问题 C++ +差异>

在各种
enum
用法中:

  • (绝对!)
    • (是的,但是为什么有这么多更好的选择呢
另见
  • 有效Java第二版,第6章:枚举和注释
    • 第30项:使用枚举而不是
      int
      常量
    • 第31项:使用实例字段而不是序号
    • 第32项:使用
      EnumSet
      代替位字段
    • 第33项:使用
      EnumMap
      代替顺序索引
API链接

这是将原始代码段从
枚举
重写为
;它捕获了原始代码段的大部分功能。出于教学目的,我还重写了字符串连接部分

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++对应的类型

工具书类
相关问题 C++ +差异>

在各种
enum
用法中:

  • (绝对!)
    • (是的,但是为什么有这么多更好的选择呢
另见
  • 有效Java第二版,第6章:枚举和注释
    • 第30项:使用枚举而不是
      int
      常量
    • 第31项:使用实例字段而不是序号
    • 第32项:使用
      EnumSet
      代替位字段
    • 第33项:使用
      EnumMap
      代替顺序索引
API链接

你不明白什么部分?书上说了什么?这是非常简单的代码…实际上,答案是
JohnDoe先生
你不明白什么部分?书上说了什么?这是非常简单的代码…实际上,答案是
JohnDoe先生
非常描述性。谢谢你的时间。非常描述性。谢谢谢谢你的时间。