java里,enum对象的反射创建
2008-01-26 20:13
465 查看
Java 的 enum 是 singleton class (只能有一个 instance)。Java 通过编译器和 JVM 联手来防止enum 产生超过一个class:不能利用 new、clone()、de-serialization、以及 Reflection API 来产生enum 的 instance。
所以想通过反射来创建枚举型对象则需要其他途径:
举个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
red,blue,green,black,white;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果用Class.newInstance()来创建,运行时肯定会报异常。
但Java也给我们准备了Enum类,可以它直接生成enum的对象。这里主要是用了它提供的方法:Enum.valueOf(Class,value);
示例如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
red,blue,green,black,white;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class RefColor ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Color color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TestEnum ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RefColor obj=new RefColor();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Class clazz=Class.forName("Color");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Field field=obj.getClass().getDeclaredField("color");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
field.set(obj, Enum.valueOf(clazz, "red"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(obj.color);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
所以想通过反射来创建枚举型对象则需要其他途径:
举个例子:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
red,blue,green,black,white;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果用Class.newInstance()来创建,运行时肯定会报异常。
但Java也给我们准备了Enum类,可以它直接生成enum的对象。这里主要是用了它提供的方法:Enum.valueOf(Class,value);
示例如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public enum Color ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
red,blue,green,black,white;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class RefColor ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Color color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TestEnum ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RefColor obj=new RefColor();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Class clazz=Class.forName("Color");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Field field=obj.getClass().getDeclaredField("color");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
field.set(obj, Enum.valueOf(clazz, "red"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(obj.color);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- Java通过反射创建对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- protobuf在java应用中通过反射动态创建对象
- Java之反射类的构造函数,通过单元测试反射创建类的对象
- java基础-反射3(反射,反射创建对象,操作对象属性,调用对象方法)
- [原创] Java JDBC连接数据库,反射创建实体类对象并赋值数据库行记录(支持存储过程)
- java使用反射创建对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- java小结_019_反射创建对象
- java 通过反射创建对象
- protobuf在java应用中通过反射动态创建对象
- java通过暴力反射创建不可实例化类的对象实例
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- Java通过反射创建对象
- 【java高级特性之反射】创建Class对象的三种方法
- Android 在Java代码中设置style属性--使用代码创建ProgressBar对象(反射机制)
- java 反射创建对象并传入参数
- java 反射应用---创建对象
- 番外 01:Spring IoC 实现原理简析,Java的反射机制,通过类名创建对象
- java反射之根据全类名创建对象