SpringMVC(五)

过了个年,今天重新看了下关于枚举的一些知识,接下来就来说说其中的一些东西,以及我犯的错误。

 

1、首先我是使用的是下面这种方式进行枚举的:

……

SMALL("S"),
MEDIUM("M"),
LARGE("L"),
EXTRA_LARGE("XL");

……

这种方式很简单,在之前的(四)中也说过了,可以使用values的方法将其中的值取出来,但是其中需要注意的是一些必要的条件。

(1)需要定义一个私有变量:

private String abbreviation;

(2)需要创建一个构造函数

Size(int code, String abbreviation) {
this.abbreviation = abbreviation;
}

(3)到此为止我们可以进行枚举值的输出:

代码如下:

package EnumTest;

import java.util.Scanner;

/**
* V1.0.0
* Created by 。。。。。 on 2017/2/3.
*/
public class EnumTest {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Entera a size:(SMALL, MEDIUM, LARGE, EXTRA_LRGE)");
String input = in.next().toUpperCase();
Size size = Enum.valueOf(Size.class, input);
System.out.println("Size=" + size);
System.out.println("abbreviation=" + size.getAbbreviation());
if(size == Size.EXTRA_LARGE){
System.out.println("Good job--you paid attention to the -.");
}
}

public enum Size {
SMALL("S"),
MEDIUM("M"),
LARGE("L"),
EXTRA_LARGE("XL");

private String abbreviation;
private Size(String abbreviation) {
this.abbreviation = abbreviation;
}

public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
}
}

但是今天想说的并不是这个,而是另外一个问题,如下:

有时候我们在列举枚举值的时候, 我们需要给定一个数字值来代替我们所列举的文字,接下来我就来说下我总结的。

我创建了三个类:

 

(1)创建一个枚举类:

package EnumTest;

/**
* V1.0.0
* Created by.....on 2017/2/3.
*/
public enum Size {
SMALL(1, "S"),
MEDIUM(2, "M"),
LARGE(3,"L"),
EXTRA_LARGE(4,"XL");

private int code;
private String abbreviation;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

Size(int code, String abbreviation) {
this.code = code;
this.abbreviation = abbreviation;
}

@Override
public String toString() {
return "Size{" +
"code=" + code +
", abbreviation='" + abbreviation + '\'' +
'}';
}
}

(2)创建一个RO来定义需要进行操作的一个定义

 

package EnumTest;

import java.io.Serializable;

/**
* V1.0.0
* Created by ..... on 2017/2/3.
*/
public class EnumTestRO implements Serializable {

private static final long serialVersionUID = -7556617260453272471L;

private Integer code;
private String abbreviation;

public static long getSerialVersionUID() {
return serialVersionUID;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

@Override
public String toString() {
return "EnumTestRO{" +
"code=" + code +
", abbreviation='" + abbreviation + '\'' +
'}';
}
}

(3)创建一个方法来进行枚举值的操作

 

package EnumTest;

/**
* V1.0.0
* Created by.....on 2017/2/3.
*/
public class outGet {
public static void main(String[] args){
for(Size a : Size.values()){
EnumTestRO aa = new EnumTestRO();
aa.setAbbreviation(a.getAbbreviation());
aa.setCode(a.getCode());
System.out.println(aa);
}
}
}

(4)如果按照上面这种写法是没有问题的,但是我在写枚举类的时候有个小细节我没有注意到,导致在运行代码的过程中出现了一个报错(Error:(*, *) java: 不兼容的类型: int无法转换为java.lang.String):

出现这个问题的主要原因是我在构造函数的时候没有注意到顺序所以导致了问题的出现:

    Size(int code, String abbreviation) {
this.code = code;
this.abbreviation = abbreviation;
}

这个地方的入参顺序一定要注意不能错,要和枚举定义顺序是一致的才行,就是int类型和String类型的顺序不能错一定要注意!!!

(5)运行结果:

 

 

 


 

 



posted @ 2017-02-03 17:13  M_派森  阅读(231)  评论(0编辑  收藏  举报