JAVA foundation-enumeration, three ways of enumeration realization, common enumeration class methods, enumeration notes

One, implement enumeration class yourself

Enumeration overview : refers to listing the values ​​of the variables one by one, and the values ​​of the variables are limited to the range of the listed values.
Example: There are only 7 days in a week, 12 months in a year, etc.
Recall the singleton design pattern: a singleton class is a class with only one instance

So many instance classes are multiple instances of a class, but not an infinite number of instances, but a finite number of instances. This can be an enumeration class.
Format 1: (No parameter structure)

public class Week1 {
    
    
	public static final Week1 MON =new Week1();
	public static final Week1 TUE =new Week1();
	public static final Week1 WED =new Week1();
	//public static final Week MON =new Week();
	private Week1() {
    
    }	//私有构造,不让其他类创建本类对象		
}

test:

		Week1 mon =Week1.MON;
		Week1 tue =Week1.TUE;
		Week1 wed =Week1.WED;
		System.out.println(mon);

Format 2: (Construction with parameters)

public class Week2 {
    
    
	public static final Week2 MON =new Week2("星期一");
	public static final Week2 TUE =new Week2("星期二");
	public static final Week2 WED =new Week2("星期三");
	
	private String name;
	private Week2(String name) 
	{
    
    
		this.name =name;
	}	//私有构造,不让其他类创建本类对象	
	public String getName() {
    
    
		return name;
	}

test:

		Week2 tue=Week2.TUE;
		System.out.println(tue.getName());
		//输出结果为星期二

Format 3: (define abstract method)
Because it is an abstract class, an object cannot be generated, so we can create a subclass of it, but the subclass cannot call the constructor of the parent class,
so we use an anonymous inner class. The braces are equivalent to inheriting this class, and the parent class refers to it. Point to the subclass object, the subclass overrides the method of the parent class

public abstract class Week3 {
    
    
	//因为是抽象类,所以不能生成一个对象,所以我们可以创建它子类,但是子类也不能调用父类的构造函数,
	//所以我们使用匿名内部类,花括号内相当于继承这个类,父类引用指向子类对象,子类重写父类的方法
	public static final Week3 MON =new Week3("星期一") {
    
    
		@Override
		public void show() {
    
    
		System.out.println("星期一");
			
		}		
	};
	public static final Week3 TUE =new Week3("星期二") {
    
    

		@Override
		public void show() {
    
    
			System.out.println("星期二");
			
		}
		
	};
	public static final Week3 WED =new Week3("星期三") {
    
    

		@Override
		public void show() {
    
    
			System.out.println("星期三");
			
		}
		
	};
	
	private String name;
	private Week3(String name) 
	{
    
    
		this.name =name;
	}	//私有构造,不让其他类创建本类对象	
	public String getName() {
    
    
		return name;
	}
	public abstract void show();
}

test:

		Week3 mon =Week3.MON;
		mon.show();
		//输出结果为今天是星期一

Two, JDK5 new features (enumeration class realized through enum)

Implement enumeration class through enum
Method 1: (No parameter structure)

public enum Week {
    
    
	MON,TUE,WEN;
}

test:

	Week mon =Week.MON;
	System.out.println(mon);

Method two: (structure with parameters)

public enum Week2 {
    
    
	MON("星期一"),TUE("星期二"),WED("星期三");
	private String name;
	private Week2(String name) {
    
    
		this.name =name;
	}
	public String getName() {
    
    
		return name;
	}	
}

test:

		Week2 mon =Week2.MON;
		System.out.println(mon.getName());

Way three: (define abstract method)

public enum Week3 {
    
    
	MON("星期一"){
    
    
		public void show() {
    
    
			System.out.println("星期一");
		}
	},TUE("星期二"){
    
    
		public void show() {
    
    
			System.out.println("星期二");
		}
	},WED("星期三"){
    
    
		public void show() {
    
    
			System.out.println("星期三");
		}
	};
	
	private String name;
	private Week3(String name) {
    
    
		this.name =name;
	}
	public String getName() {
    
    
		return name;
	}
	public abstract void show();
}

test:

		Week3 mon =Week3.MON;
		mon.show();

Three, JDK5 new features (notes for enumeration)

  • Use the keyword enum to define an enumeration class
  • All enumeration classes are subclasses of Enum
  • The first line of the enumeration class must be an enumeration item. The semicolon after the last enumeration item can be omitted, but if the enumeration class has other things, this semicolon cannot be omitted. Suggest not to omit
  • An enumeration class can have a constructor, but it must be private, and its default is also private.
  • Enumeration classes can also have abstract methods, but enumeration items must override this method
  • Use of enumeration in switch statement
Week3 day=Week3.MON;
//可以修改Week3.后面的值
		switch (day) {
    
    
		case MON:
			System.out.println("星期一");
			break;
		case TUE:
			System.out.println("星期二");
			break;
		case WED:
			System.out.println("星期三");
			break;
		}

Four, JDK5 new features (common methods of enumeration classes)

int ordinal() method
Returns the ordinal number of the enumeration constant (its position in the enumeration declaration, where the initial constant ordinal bit is 0)

		Week2 mon =Week2.MON;
		Week2 tue =Week2.TUE;
		Week2 wed =Week2.WED;
//枚举都是有编号的
		System.out.println(mon.ordinal());
		System.out.println(tue.ordinal());
		System.out.println(wed.ordinal());

Output effect:
Insert picture description here
int compareTo(E o) method
Compare the position of enum constants

		Week2 mon =Week2.MON;
		Week2 tue =Week2.TUE;
		Week2 wed =Week2.WED;
		System.out.println(mon.compareTo(tue));
//比较的是编号
		System.out.println(mon.compareTo(wed));

The effect is as follows:
Insert picture description here
String name() method
Output the name of the enum constant

Week2 mon =Week2.MON;
System.out.println(mon.name());

The effect is as follows:
Insert picture description here
String toString() method
The first step: write enum toString method

public enum Week2 {
    
    
	MON("星期一"),TUE("星期二"),WED("星期三");
	private String name;
	private Week2(String name) {
    
    
		this.name =name;
	}
	public String getName() {
    
    
		return name;
	}
	public String toString() {
    
    	
		return name;
	}
}
Week2 mon =Week2.MON;
System.out.println(mon.toString());

The effect is as follows:
Insert picture description here

<generic> T valueOf(Class<generic> type,String name)

Obtain the enumeration item object from the bytecode file and pass in a name. It can be replaced, if TUE is passed in, it will return to Tuesday

//通过字节码文件获取枚举项对象,传入一个名称。可以更换,如果传入TUE就返回星期二
		Week2 mon =Week2.valueOf(Week2.class,"MON");
		Week2 tue =Week2.TUE;
		Week2 wed =Week2.WED;
		System.out.println(mon);

The effect is as follows:
Insert picture description here
values()

Week2[]arr =Week2.values();
		for (Week2 week2 : arr) {
    
    
			//也可以通过week2获取getName方法
			System.out.println(week2);
		}

Insert picture description here

Five, JDK7 features

  • Binary literal
  • Number literals can appear underlined
System.out.println(0b110);//十进制数是6
System.out.println(100_000);//类似10,000看起来方便
  • The switch statement can use strings
  • Generic simplification, diamond generic
  • Multiple catch merges of exceptions, each exception uses or |
  • try-with-resources statement (you can use parentheses to automatically close the flow)

Six, JDK8

  • A method with a method body can be defined in the interface. If it is non-static, it must be modified with default
  • If it is static, no need
public static void main(String[] args) {
    
    
	//Demo demo=new Demo();
	//demo.print();
	//Inter.method();
	
	Demo demo =new Demo();
	demo.run();
}
}

interface Inter{
    
    
	public default void print() {
    
    
		System.out.println("Hello World");
	}
	
	public static void method() {
    
    
		System.out.println("static method");
	}
	
}
class Demo implements Inter{
    
    
	public void run() {
    
    
		//jdk1.7版本以前必须要使用final修饰 , jdk1.8系统默认设置num必须为final,不允许修改
		int num =10;
		class Inner{
    
    
			public void fun() {
    
    
				
				System.out.println(num);
				System.out.println("fun");
			}
		}
		Inner inner =new Inner();
		inner.fun();
	}

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/108953402