您的位置:首页 > 其它

关于反射的一些程序

2012-09-20 19:25 225 查看
1.

import java.lang.reflect.*;

import java.util.Date;

public class ArgCITest {

public static void main(String[]

args) {

try {

Class

clazz=Class.forName("java.util.Date");

Constructor

constructor=clazz.getConstructor

(long.class);

Date d=(Date)

constructor.newInstance(1234L);

System.out.println(d);

} catch (Exception e) {

e.printStackTrace

();

}

}

}

2.

import java.beans.IntrospectionException;

import java.beans.PropertyDescriptor;

import

java.lang.reflect.InvocationTargetExceptio

n;

import java.lang.reflect.Method;

public class IntroSRTest {

public static void main(String[]

args) throws Exception {

ReflectPoint pt=new

ReflectPoint(3,7);

String propertyName="x";

// x-->X-->getX--->Method

PropertyDescriptor pd =

getXXX(pt, propertyName);

int a=10;

setXXX(pt, propertyName,

a);

}

private static void setXXX

(ReflectPoint pt, String propertyName, int

a)

throws

IntrospectionException,

IllegalAccessException,

InvocationTargetException {

PropertyDescriptor pd1=new

PropertyDescriptor(propertyName,

pt.getClass());

Method

methodSetX=pd1.getWriteMethod();

methodSetX.invoke(pt, a);

}

private static PropertyDescriptor

getXXX(ReflectPoint pt,

String

propertyName) throws

IntrospectionException,

IllegalAccessException,

InvocationTargetException {

PropertyDescriptor pd=new

PropertyDescriptor(propertyName,

pt.getClass());

Method

methodGetX=pd.getReadMethod();

Object

returnValue=methodGetX.invoke(pt);

System.out.println

(returnValue);

return pd;

}

}

3.

import java.util.Date;

public class NoArgCITest {

public static void main(String[]

args) {

try {

Class

c=Class.forName("java.util.Date");

Date d=(Date)

c.newInstance();

Date currentDate=

(Date)newInstance("java.util.Date");

System.out.println(currentDate);

} catch (Exception e) {

// TODO Auto-

generated catch block

e.printStackTrace

();

}

}

private static Object newInstance

(String string) {

Object obj=null;

try {

obj=Class.forName

(string).newInstance();

} catch (Exception e) {

// TODO Auto-

generated catch block

e.printStackTrace

();

}

return obj;

}

}

4.

import java.lang.reflect.Array;

public class RATest {

public static void main(String[]

args) {

/*int[] i=new int[5];

Integer a=new Integer(5);

System.out.println

(i.getClass().getComponentType().getName

());

System.out.println

(a.getClass().getName());*/

Object

obj=Array.newInstance(int.class, 5);

for(int i=0;i<5;i++){

Array.setInt(obj,

i, i*10);

}

for(int i=0;i<5;i++){

System.out.println(Array.getInt(obj, i));

}

}

}

5.

public class ReflectPoint {

private int x;

private int y;

public ReflectPoint(int x,int y){

this.x=x;

this.y=y;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

System.out.println(x);

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

System.out.println(y);

}

}

6.

import java.lang.reflect.*;

public class RFTest {

public static void main(String[]

args) throws Exception{

Class clazz=Class.forName

("com.second.Product");

Product pd=(Product)

clazz.newInstance();

Field

idField=clazz.getDeclaredField("id");

idField.setAccessible

(true);

idField.setLong(pd, 100);

System.out.println

(idField.getLong(pd));

Field

nameField=clazz.getDeclaredField("name");

nameField.setAccessible

(true);

nameField.set(pd, "张三");

System.out.println

(nameField.get(pd));

}

}

7.

import java.lang.reflect.Method;

public class RIMTest {

public static void main(String[]

args) throws Exception {

Class clazz=Class.forName

("com.second.Product"); // 生成class对象

Product pd=(Product)

clazz.newInstance(); //创建Product类的对



Method

method1=clazz.getDeclaredMethod("setName",

String.class); //调用public方法、有参数

Object

returnValue=method1.invoke(pd, "爪哇");

System.out.println

(returnValue);

Method

method2=clazz.getDeclaredMethod

("display"); //调用private方法、无参数

method2.setAccessible

(true);

Object

returnValue1=method2.invoke(pd);

}

}

class Product{

private static long count=0; //

私有类成员变量

private long id;

private String name="无名氏";

public Product(){ //构造方法

System.out.println("默认的

构造方法");

id=++count;

}

public long getId() {

return id;

}

public void setId(long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

System.out.println("调用

setName方法");

}

private void display(){

System.out.println

(getClass().getName()+"[id="+id

+",name="+name+"]");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: