Enumerated Interface Implementation

Keywords: Spring Java JDK

Links to the original text: https://blog.csdn.net/u012291108/article/details/51509988

Java Enumeration Classes and Custom Enumeration Classes and enum Declarations and Implementation Interfaces

1. Enumeration classes

Note:
Before JDK 1.5, you need to customize the enumeration class
JDK 1.5 enum keyword is added to define enumeration classes
If enumeration has only one member, it can be implemented as a singleton pattern

1. Attributes of enumerated classes

  1. The properties of enumerated class objects should not be allowed to be changed, so private final modification should be used.
  2. Attributes of enumerated classes modified with private final should be assigned to them in the constructor
  3. If the enumeration class explicitly defines a constructor with parameters, the corresponding incoming parameters must also be assigned when enumeration values are listed.

2. Custom Enumeration Classes

  1. How to customize the methods of enumeration classes written in comments
//Custom Enumeration Class
class Season {
	//1. Provide class properties declared as private final
	private final String seasonName;
	private final String seasonDesc;
	//2. Attributes declared final, initialized in the constructor
	private Season(String seasonName,String seasonDesc) {
		this.seasonName = seasonName;
		this.seasonDesc = seasonDesc;
	}
	//3. Calling attributes through common methods
	public final String getSeasonName() {
		return seasonName;
	}
	public final String getSeasonDesc() {
		return seasonDesc;
	}
	//4. Create the object of the enumeration class: declare the object of the class as public static final
	public static final Season SPRING = new Season("spring", "In the warm spring, flowers are coming out with a rush");
	public static final Season SUMMER = new Season("summer", "Dog Days of Summer");
	public static final Season FALL = new Season("Autumn", "Much has been achieved.");
	public static final Season WINTER = new Season("winter", "Snow gleams white");
<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">"This season"</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
  1. 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 Class

  1. Enumerated class objects must be declared on the first line of the enumerated class.
  2. The difference between enumeration class and common class:
    ①Use enum Defined enumeration classes inherit by default java.lang.Enum class
    ②Enumeration class constructors can only be used private Access Controller
    ③All instances of an enumerated class must be explicitly listed in the enumerated class(, Separate ; Ending). The example system listed will be automatically added public static final Modification
  3. JDK 1.5 It can be found in the switch Use in expressions Enum Objects of defined enumeration classes as expressions, case The clause can use the name of the enumerated value directly, There is no need to add enumeration classes as qualifications
  4. Dome
interface info{
	void show();
}
//Enum Class
enum Season1  implements info{
	SPRING("spring", "In the warm spring, flowers are coming out with a rush"){
	public void show() {
		System.out.println("Where is spring?");
	}
	},
	SUMMER("summer", "Dog Days of Summer")
	{
		public void show() {
			System.out.println("Living like summer flowers");
		}
	}	,
	AUTUTO("Autumn", "Much has been achieved.")
	{
		public void show() {
			System.out.println("Autumn is the season for breaking up.");
		}
	},
	WINTER("winter", "Snow gleams white")
	{
		public void show() {
			System.out.println("A fire in winter");
		}
	};
<span class="token comment">//1. Provide class properties declared as 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. Attributes declared final, initialized in the constructor</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. Calling attributes through common methods</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("this season");
// }
}

  • 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
  1. Test
    Common methods are written in notes
public class TestEnum {
	public static void main(String[] args) {
		System.out.println("------------enum Keyword");
		//1.values(): Returns an array of enumerated classes
		Season1 [] seasons = Season1.values();
		for(int i = 0; i < seasons.length;i++) {
			System.out.println(seasons[i]);
		}
		//2.valueOf(String name): Requires the incoming parameter name to be the name of the enumerated object
		//Otherwise: Report a java.lang.IllegalArgumentException exception
		String str = "SPRING";
		Season1 sea = Season1.valueOf(str);
		System.out.println(sea);
		//Running status of threads
		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. Enumeration Classes for Implementing Interfaces

  1. Like ordinary Java classes, enumeration classes can implement one or more interfaces.
  2. If each enumerated value is required to behave differently in the interface method invoked, then each enumerated value can be used to implement the method separately.
  3. The implementation method can be seen in the demo above.
                                </div>

Posted by everlifefree on Wed, 28 Aug 2019 04:38:35 -0700