枚举的接口实现
作者:互联网
原文链接:https://blog.csdn.net/u012291108/article/details/51509988
Java 枚举类和自定义枚举类和enum声明及实现接口
1.枚举类
注:
JDK1.5之前需要自定义枚举类
JDK 1.5 新增的 enum 关键字用于定义枚举类
若枚举只有一个成员, 则可以作为一种单例模式的实现方式
1.枚举类的属性
- 枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰
- 枚举类的使用 private final 修饰的属性应该在构造器中为其赋值
- 若枚举类显式的定义了带参数的构造器, 则在列出枚举值时也必须对应的传入参数
2.自定义枚举类
- 如何自定义枚举类的方法写在注释里
//自定义枚举类 class Season { //1.提供类的属性,声明为private final private final String seasonName; private final String seasonDesc; //2.声明为final的属性,在构造器中初始化 private Season(String seasonName,String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //3.通过公共的方法来调用属性 public final String getSeasonName() { return seasonName; } public final String getSeasonDesc() { return seasonDesc; } //4.创建枚举类的对象:将类的对象声明为public static final public static final Season SPRING = new Season("春天", "春暖花开"); public static final Season SUMMER = new Season("夏天", "夏日炎炎"); public static final Season FALL = new Season("秋天", "硕果累累"); public static final Season WINTER = new Season("冬天", "白雪皑皑");
<span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"Season [seasonName="</span> <span class="token operator">+</span> seasonName <span class="token operator">+</span> <span class="token string">", seasonDesc="</span> <span class="token operator">+</span> seasonDesc <span class="token operator">+</span> <span class="token string">"]"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"这个一个季节"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- Test
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName() +" "+spring.getSeasonDesc());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.Enum枚举类
- 必须在枚举类的第一行声明枚举类对象。
- 枚举类和普通类的区别:
①使用 enum 定义的枚举类默认继承了 java.lang.Enum 类
②枚举类的构造器只能使用 private 访问控制符
③枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾). 列出的实例系统会自动添加 public static final 修饰 - JDK 1.5 中可以在 switch 表达式中使用Enum定义的枚举类的对象作为表达式, case 子句可以直接使用枚举值的名字, 无需添加枚举类作为限定
- Dome
interface info{ void show(); } //枚举类 enum Season1 implements info{ SPRING("春天", "春暖花开"){ public void show() { System.out.println("春天在哪里?"); } }, SUMMER("夏天", "夏日炎炎") { public void show() { System.out.println("生如夏花"); } } , AUTUTO("秋天", "硕果累累") { public void show() { System.out.println("秋天是用来分手的季节"); } }, WINTER("冬天", "白雪皑皑") { public void show() { System.out.println("冬天里的一把火"); } };
<span class="token comment">//1.提供类的属性,声明为private final</span> <span class="token keyword">private</span> <span class="token keyword">final</span> String seasonName<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">final</span> String seasonDesc<span class="token punctuation">;</span> <span class="token comment">//2.声明为final的属性,在构造器中初始化</span> <span class="token keyword">private</span> <span class="token function">Season1</span><span class="token punctuation">(</span>String seasonName<span class="token punctuation">,</span>String seasonDesc<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>seasonName <span class="token operator">=</span> seasonName<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>seasonDesc <span class="token operator">=</span> seasonDesc<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//3.通过公共的方法来调用属性</span> <span class="token keyword">public</span> <span class="token keyword">final</span> String <span class="token function">getSeasonName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> seasonName<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">final</span> String <span class="token function">getSeasonDesc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> seasonDesc<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"Season [seasonName="</span> <span class="token operator">+</span> seasonName <span class="token operator">+</span> <span class="token string">", seasonDesc="</span> <span class="token operator">+</span> seasonDesc <span class="token operator">+</span> <span class="token string">"]"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
// public void show() {
// System.out.println(“这个一个季节”);
// }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- Test
常用方法写在注释里面
public class TestEnum { public static void main(String[] args) { System.out.println("------------enum关键字"); //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 = "SPRING"; Season1 sea = Season1.valueOf(str); System.out.println(sea); //线程的运行状态 Thread.State[] states = Thread.State.values(); for (int i = 0; i < states.length; i++) { System.out.println(states[i]); }
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
3.实现接口的枚举类
- 实现接口的枚举类和普通 Java 类一样,枚举类可以实现一个或多个接口
- 若需要每个枚举值在调用实现的接口方法呈现出不同的行为方式, 则可以让每个枚举值分别来实现该方法
- 实现方法可以看上面的demo。
</div>
标签:String,实现,Season,seasonDesc,接口,final,枚举,public 来源: https://blog.csdn.net/weixin_44079558/article/details/100124855