您的位置:首页 > 编程语言 > Java开发

第九章:Java_枚举类和注解

2017-02-13 22:48 295 查看

一、枚举类

1.如何自定义枚举类。 枚举类:类的对象是有限个的,确定的。

1.1 私有化类的构造器,保证不能在类的外部创建其对象

1.2 在类的内部创建枚举类的实例。声明为:public static final

1.3 若类有属性,那么属性声明为:private final 。此属性在构造器中赋值。

2.使用enum关键字定义枚举类

2.1其中常用的方法:values() valueOf(String name);

2.2枚举类如何实现接口 :①让类实现此接口,类的对象共享同一套接口的抽象方法的实现。②让类的每一个对象都去实现接口的抽象方法,进而通过类的对象调用被重写的抽象方法时,执行的效果不同

public class TestSeason1 {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());

System.out.println();
//1.values()
Season1[] seasons = Season1.values();
for(int i = 0;i < seasons.length;i++){
System.out.println(seasons[i]);
}
//2.valueOf(String name):要求传入的形参name是枚举类对象的名字。
//否则,报java.lang.IllegalArgumentException异常
String str = "WINTER";
Season1 sea = Season1.valueOf(str);
System.out.println(sea);
System.out.println();

Thread.State[] states = Thread.State.values();
for(int i = 0;i < states.length;i++){
System.out.println(states[i]);
}
sea.show();

}
}
interface Info{
void show();
}
//枚举类
enum Season1 implements Info{
SPRING("spring", "春暖花开"){
public void show(){
System.out.println("春天在哪里?");
}
},
SUMMER("summer", "夏日炎炎"){
public void show(){
System.out.println("生如夏花");
}
},
AUTUMN("autumn", "秋高气爽"){
public void show(){
System.out.println("秋天是用来分手的季节");
}
},
WINTER("winter", "白雪皑皑"){
public void show(){
System.out.println("冬天里的一把火");
}
};

private final String seasonName;
private final String seasonDesc;

private Season1(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}

@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc="
+ seasonDesc + "]";
}
//  public void show(){
//      System.out.println("这是一个季节");
//  }
}


【来源其他博文:】

枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。

1、常量的使用

在JDK1.5之前,我们定义常量都是:public static fianl….。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。

public enum Color {
RED, GREEN, BLANK, YELLOW
}


使用:

public class B {
public static void main(String[] args) {
System.out.println( isRed( Color.BLANK ) ) ;  //结果: false
System.out.println( isRed( Color.RED ) ) ;    //结果: true

}
static boolean isRed( Color color ){
if ( Color.RED.equals( color )) {
return true ;
}
return false ;
}
}


或者 switch 的使用:

public class B {
public static void main(String[] args) {
showColor( Color.RED );
}
static void showColor(Color color){
switch ( color ) {
case BLANK:
System.out.println( color );
break;
case RED :
System.out.println( color );
break;
default:
System.out.println( color );
break;
}
}
}


2、自定义函数

public enum Color {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
private String name ;
private int index ;
private Color( String name , int index ){
this.name = name ;
this.index = index ;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}


使用:

public class B {
public static void main(String[] args) {
//输出某一枚举的值
System.out.println( Color.RED.getName() );
System.out.println( Color.RED.getIndex() );
//遍历所有的枚举
for( Color color : Color.values()){
System.out.println( color + "  name: " + color.getName() + "  index: " + color.getIndex() );
}
}
}


结果:

红色

1

RED name: 红色 index: 1

GREEN name: 绿色 index: 2

BLANK name: 白色 index: 3

YELLO name: 黄色 index: 4

二、注解Annotation

1.JDK提供的常用的三个注解

@Override: 限定重写父类方法, 该注释只能用于方法

@Deprecated: 用于表示某个程序元素(类, 方法等)已过时

@SuppressWarnings: 抑制编译器警告

2.如何自定义注解

以SuppressWarnings为例进行创建即可

3.元注解:可以对已有的注解进行解释说明。



Retention: SOURCE CLASS RUNTIME

Target:

Documented:javadoc

Inherited

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: