枚举

枚举是限定有限可能值的一种手段,使用枚举可以降低程序出错的几率,并可以提高代码的可读性与可维护性。Java中的枚举并不是简单常量的集合,而是一个对象,其本质依然是类,所以Java中的枚举除了提供一系列相关值以外,还提供了一些额外功能,甚至还可以根据需要自行添加一些功能。本文拟就Java枚举的用法做一相对全面的介绍。

Java枚举提供了几个方法供开发者在需要时调用,对于枚举对象,主要可用的方法为values,它返回当前枚举中定义的所有枚举项的集合;对于枚举项,主要可用的方法有ordinal、name和getDeclaringClass。方法ordinal返回枚举项在枚举对象中的序号;方法name则返回枚举项的名称(与方法toString效果相同),通常用于取得枚举变量中保存的枚举项名称;而方法getDeclaringClass则用于取得当前枚举值所在类的完整名称。此外,枚举项之间还可以通过方法compareTo进行比较,如果参数传入的枚举项与当前值相等,则返回0。 

等价类 

package enumPackage;

public class Color1 {
	public static final Color1 Red=new Color1("红色",1); 
	public static final Color1 Green=new Color1("绿色",2); 
	public static final Color1 Yello=new Color1("黄色",3);
	private String name;
	private int index;
	private Color1(String name, int index) {
		super();
		this.name = name;
		this.index = index;
	}
	public int getIndex() {
		return index;
	}
	
	public String getName() {
		return name;
	}
	public static String getName(int index){
		for(Color1 c:Color1.valueOf()){
			if(c.getIndex()==index){
				return c.getName();
			}
		}
	
		return null;
	}
	public static Color1[] valueOf(){
		Color1[] values={Red,Green,Yello};
		return values;
	}
}

枚举进行替换,简介,安全,有了封装好的方法

package enumPackage;

public enum Color {
	Red("红色",1), Green("绿色",2), Yello("黄色",3);
	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 static String getName(int index) {
		Color[] values = Color.values();
		for (int i = 0; i < values.length; i++) {
			if(values[i].getIndex()==index){
				return values[i].getName();
			}
		}
		return null;
	}
}
package enumPackage;

public class Test {

	public static void main(String[] args) {
		Color[] values = Color.values();
		for (int i = 0; i < values.length; i++) {
			System.out.print(values[i].ordinal());
			System.out.println(" "+values[i].toString());
			
			//String str=value[i].toString();
			//System.out.println(str);
		}
		System.out.println(Color.getName(2));
		System.out.println("----------");
		System.out.println(Color1.getName(2));
		
		/*
		Color b = Color.valueOf(Color.class,"Green");
		System.out.println(b);
		Color a = Color.valueOf("Red");
		System.out.println(a);
		*/
		
		WeekDay c = WeekDay.valueOf("Sun");
		System.out.println(c.next().name());
		
	}

}

Javabean

JavaBean定义了一组规则

JavaBean就是遵循此规则的平常的Java对象  

满足三个条件:  

执行java.io.Serializable 接口 

2.提供无参数的构造器 

提供getter 和 setter方法访问它的属性. 

JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。

如果要你自己去通过getX方法来访问私有的x,怎么做,有一定难度吧?用内省这套api操作JavaBean比用普通类的方式更方便。

PropertyDescriptor描述 Java Bean 通过一对存储器方法导出的一个属性。

package beanpackage;

import java.util.Date;

public class Person {
	private String name;
	private int age;
	private Date birthday=new Date();
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}


package beanpackage;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyPropertyDescriptor {

	public static void main(String[] args) throws Exception{
		//Person类中有name和age属性
		Object obj=new Person();
		String propertyName = "age";
		Object value=3;
	/*	String propertyName = "name";
		Object value="张三";*/
		setProperty(obj, propertyName, value);
		Object o = getProperty(obj, propertyName);
		System.out.println(o);
	}

	private static void setProperty(Object obj, String propertyName,
			Object value) throws IntrospectionException,
			IllegalAccessException, InvocationTargetException {
		Class<? extends Object> beanClass = obj.getClass();
		PropertyDescriptor propertyDescriptor=new PropertyDescriptor(propertyName, beanClass);
		Method methodSet = propertyDescriptor.getWriteMethod();
		methodSet.invoke(obj,value);
		//Person p=(Person) object;
		//System.out.println(p.getName());
		//System.out.println(p.getAge());
	}

	private static Object getProperty(Object obj, String propertyName)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		Class<? extends Object> beanClass = obj.getClass();
		PropertyDescriptor propertyDescriptor1=new PropertyDescriptor(propertyName, beanClass);
		Method methodGet = propertyDescriptor1.getReadMethod();
		Object object = methodGet.invoke(obj, null);
		return object;
	}
}

Beanutils工具包 

用eclipse如何加入jar包,先只是引入beanutils包,等程序运行出错后再引入logging包。

直接操作Javabean太繁琐,可以直接导入Beanutils工具包,这样就方便使用。

使用方法:先下载压缩包,解压。解压后里面有API和文件。查阅API可以了解如何使用。然后拷贝jar包。为了使用户可以调用到这个jar包,新建一个Folder(目录),名为lib,然后将jar包拷贝到这个目录下,然后右键选择AddBuildpath,即可。

注意:尽量全部拷入,防止出现缺少jre文件的异常。

package beanpackage;

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsDemo1 {

	public static void main(String[] args) throws Exception {
		Person p=new Person();
		BeanUtils beanUtils=new BeanUtils();
		beanUtils.setProperty(p, "name", "张三");
		System.out.println(beanUtils.getProperty(p, "name"));
		
		beanUtils.setProperty(p, "birthday.time", "100");
		System.out.println(beanUtils.getProperty(p, "birthday.time"));
	}

}