Java从入门到精通 - 反射Constructor构造函数的使用
来源:互联网 发布:西安交大图书馆网络 编辑:程序博客网 时间:2024/04/28 13:18
import java.lang.reflect.Constructor;
//请注意执行顺序问题,开始时,我测试时用的是MyEclipse中的Jdk1.5.Jdk1.6我发现他们Constructor的顺序不一样的。
public class ReflectTest {
String s;
int i,i2,i3;
protected ReflectTest()
{
s="";
i=0;
i2=0;
i3=0;
}
protected ReflectTest(String s,int i)
{
this.s = s;
this.i = i;
}
public ReflectTest(String...strings) throws NumberFormatException //可变数量的参数
{
if(0<strings.length)
{
i = Integer.valueOf(strings[0]);
}
if(1<strings.length)
{
i2 = Integer.valueOf(strings[1]);
}
if(2<strings.length)
{
i3 = Integer.valueOf(strings[2]);
}
}
public void print()
{
System.out.println("s= "+s);
System.out.println("i="+i);
System.out.println("i2= "+i2);
System.out.println("i3="+i3);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Constructor []declaredConstructor = ReflectTest.class.getDeclaredConstructors();//获得所有的构造方法
for(int i=0;i<declaredConstructor.length;i++)
{
Constructor constructor = declaredConstructor[i];
System.out.println("查看是否允许带有可变数量的参数:"+constructor.isVarArgs());//是否允许带有可变数量的参数
System.out.println("该构造方法的入口参数类型依次为:");
Class [] parameterTypes = constructor.getParameterTypes();
for(int j=0;j<parameterTypes.length;j++)//构造方法的入口参数类型
{
System.out.println("parameterTypes["+j+"]"+ parameterTypes[j]);
}
System.out.println("该构造方法可能抛出的异常类型:");
Class []exceptionTypes = constructor.getExceptionTypes();
for(int j=0;j<exceptionTypes.length;j++)//构造方法可能抛出的异常类型
{
System.out.println("exceptionTypes["+j+"]"+ exceptionTypes[j]);
}
ReflectTest reflectTest = null;
while(reflectTest ==null) //调用类中的构造方法
{
try
{
if(i==0) //请注意执行顺序,可以先把这些语句注释掉,看下顺序再执行下面的代码.
{
reflectTest=(ReflectTest)constructor.newInstance();
}
else if(i==2)
{
reflectTest=(ReflectTest)constructor.newInstance("7",5);
}
else if(i==1)
{
Object[] parameters = new Object[]{new String[]{"100","200","300"}};
//Object []parameters = new String[]{"100","200","300"};
reflectTest =(ReflectTest)constructor.newInstance(parameters);
}
}catch(Exception e)
{
System.out.println("在创建对象时抛出异常:下面执行setAccessible");
e.printStackTrace();
constructor.setAccessible(true);
}
}
reflectTest.print();
System.out.println();
System.out.println("*********************");
}
}
/**
* @return the s
*/
protected String getS() {
return s;
}
/**
* @param s the s to set
*/
protected void setS(String s) {
this.s = s;
}
/**
* @return the i
*/
protected int getI() {
return i;
}
/**
* @param i the i to set
*/
protected void setI(int i) {
this.i = i;
}
/**
* @return the i2
*/
protected int getI2() {
return i2;
}
/**
* @param i2 the i2 to set
*/
protected void setI2(int i2) {
this.i2 = i2;
}
/**
* @return the i3
*/
protected int getI3() {
return i3;
}
/**
* @param i3 the i3 to set
*/
protected void setI3(int i3) {
this.i3 = i3;
}
}
- Java从入门到精通 - 反射Constructor构造函数的使用
- java从入门到精髓 - 反射Constructor
- Java从入门到精通 - 反射Field
- Java从入门到精通 - 反射Method
- Java从入门到精通 - 反射Annotation
- java反射之使用Constructor调用私有构造函数
- 真正的java从入门到精通。
- JAVA WEB从入门到精通day08 JUnit和反射的学习
- JAVA从入门到精通
- JAVA从入门到精通
- JAVA从入门到精通
- java 从入门到精通
- Java从入门到精通
- java从入门到精通
- java从入门到精通
- Java从入门到精通
- java从入门到精通
- Class.forName java.lang.reflect.Constructor反射调用有参构造函数和无参构造函数的方法
- JFreeChart :快速散点图演示
- hdu 2027 统计元音
- 经验分享
- cegui图片文字写屏
- JSP页面中JS,图片等相对路径引用问题
- Java从入门到精通 - 反射Constructor构造函数的使用
- 引用kernel32.dll实现简体字和繁体字转换
- Rhel 4 下单独装pdo_mysql
- 可以利用的资源
- sqlite教程网址
- dnn(dotnetnuke)下如果实现二级域名的单点登录
- ERP业务流程优化设计之思想和原则
- 网络通信程序的开发入门(Winsock)
- 程序员从初级到中级10个秘诀