Java的Enum用法记录

用法一:

NameCompletor:

public enum NameCompletor {
	MALE {
		@Override
		public String getFullName(String name) {
			return "Mr." + name;
		}
	},
	FEMALE {
		@Override
		public String getFullName(String name) {
			return "Mrs." + name;
		}
	};

	public String getFullName(String name) {
		throw new AbstractMethodError();
	}

}

NamePrinter:

public class NamePrinter {

	public static void print(String name, NameCompletor completor) {
		System.out.println("Hello, " + completor.getFullName(name));
	}

}

Example1Main:

public class Example1Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NamePrinter.print("Mike", NameCompletor.MALE);
		NamePrinter.print("Lily", NameCompletor.FEMALE);
	}

}

输出结果如下:

Hello, Mr.Mike
Hello, Mrs.Lily

你可能感兴趣的:(java,enum)