关于反射的一些程序
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+"]");
}
}
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+"]");
}
}
相关文章推荐
- 关于作为IT行业中程序开发人员的一些反思!!!
- 黑马程序员-关于反射的一些小总结
- CSDN上下载的一些关于Android程序调用Webservice执行不成功的问题
- 关于效率、程序与生活的一些思考
- 关于程序调用水晶报表的一些经验
- 关于程序自动安装软件的一些问题1
- 关于程序的bss段,堆区,栈区的一些学习。
- 关于小程序,一些想法
- 关于微信小程序的一些看法
- 黑马程序员-关于反射的一些小总结
- 关于微信小程序开发的一些细节
- Unix:关于淘宝tsar框架中的一些程序
- Java的一些高级特性(三)——关于反射
- 关于反射的一些内容
- 关于监控程序启动和关闭的一些想法
- C语言中关于数字的一些小程序
- J2ME中关于网络程序中的一些总结
- CSDN上下载的一些关于Android程序调用Webservice运行不成功的问题
- 关于“设计模式”和“设计程序语言”的一些闲话
- 关于反射的一些姿势