黑马程序员技术交流社区
标题:
关于Enum的问题,以及反射,和暴力反射
[打印本页]
作者:
728503435
时间:
2015-6-2 22:28
标题:
关于Enum的问题,以及反射,和暴力反射
public enum Weekday {
MON("星期一"), TUE("星期二"), WED("星期三"), THU("星期四"), FRI("星期五"), SAR("星期六"), SUN(
"星期天");
private Weekday(String day) {
this.day = day;
}
private String day;
public String getDay(){
return this.day;
}
public void setDay(String day) {
this.day = day;
}
}
==============================================================================
package DemoWeekday;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
public class WeekdayTest {
@Test
public void show() throws IntrospectionException {
// getDay(Weekday.FRI);
BeanInfo bi = Introspector.getBeanInfo(Weekday.class);
PropertyDescriptor[] pd = bi.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : pd) {
System.out.println(propertyDescriptor.getName());
}
}
@Test
public void show1() throws Exception {
PropertyDescriptor pd=new PropertyDescriptor("day", Weekday.class);
Method me=pd.getReadMethod();
System.out.println(me.invoke(Weekday.MON,null ));
}
public void getDay(Weekday WD) {
System.out.println(WD.getDay());
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2