您的位置:首页 > 编程语言 > Java开发

JAVA反射demo例子

2013-12-04 09:44 274 查看
public class ReflectPoint {

private int x;
public int y;

public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";

public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

 

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
/*
* HashSet 先判断是否存在该对象,如果存在则不放,
* ArrayList是有顺序的,可以任意改变序号,人一个都可以放进去
*/
public class ReflectTest2 {

public static void main(String[] args) throws Exception{

/*
* 配置文件的路径应该是相对与工程的目录,跟class和源文件的位置没有关系
* 一定要用完整的路径,地址是运算出来的
*/

InputStream ips = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(ips);
ips.close();
String str = props.getProperty("className");
Collection conllections = (Collection)Class.forName(str).newInstance();

//Collection conllections = new ArrayList();
//Collection conllections = new HashSet();
ReflectPoint pt1 =new ReflectPoint(3,3);
ReflectPoint pt2 =new ReflectPoint(5,5);
ReflectPoint pt3 =new ReflectPoint(3,3);
conllections.add(pt1);
conllections.add(pt2);
conllections.add(pt3);
conllections.add(pt1);
System.out.println(conllections.size());
}
}

 

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ReflectTest {

public static void main(String[] args) throws Exception{

String str1 = "abc";
Class cls1 = str1.getClass();
Class cls2 = String.class;
Class cls3;

cls3 = Class.forName("java.lang.String");
//字节码都是指向同一份——内存中的那个,所以下面的全部相等
System.out.println(cls1 == cls2);
System.out.println(cls2 == cls3);

//判断某个类是否是基本类型
System.out.println(cls1.isPrimitive());
System.out.println(int.class.isPrimitive());

System.out.println(int.class == Integer.class);
//TYPE属性反映的是基本类型的那份字节码
System.out.println(int.class == Integer.TYPE );

//数组也是一种类型
System.out.println(int[].class.isPrimitive());
//判断某个对象或者类是否是数组类型用isArray()方法
System.out.println(int[].class.isArray());

//new String(new StringBuffer("dsaf"));
//Class --> Constructor --> Object
//获得方法时要用的类型
Constructor construct1 = String.class.getConstructor(StringBuffer.class);
String str2 = (String)construct1.newInstance(new StringBuffer("abc"));
System.out.println(str2);

ReflectPoint pt1 = new ReflectPoint(3,5);
Field fieldY = pt1.getClass().getField("y");
//fieldY不是对象上的变量,而是类上,要用它去取某个对象上对应的Z值
System.out.println(fieldY.get(pt1));

Field fieldX = pt1.getClass().getDeclaredField("x");//让某个对象
fieldX.setAccessible(true);//
System.out.println(fieldX.get(pt1));

//更改对象里面的属性,将a改为b
changeStringValue(pt1);
System.out.println("pt1.str1:" + pt1.str1);
System.out.println("pt1.str2:" + pt1.str2);
System.out.println("pt1.str3:" + pt1.str3);

//str.charAt(1)
//包名如果是用java打头的是标准,如果是用com打头则是公司所用
Method methodCharAt = String.class.getMethod("charAt", int.class);
System.out.println(methodCharAt.invoke(str1, 2));
System.out.println(methodCharAt.invoke(str1, new Object[]{2}));

//TestArguments.main(new String[]{new String("123"),new String("abc"),new String("xyz")});
String startingClassName = args[0];
Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
/*
* 这里必须封装一层Object数组,在invoke方法中会拆开,然后出现对象new String[]{new String("123"),
* new String("abc"),new String("xyz")}一个string[]数组对象
* 如果没有封装,则在invoke方法中直接拆开则参数会是new String("123"),new String("abc"),new String("xyz")三个对象
*/
mainMethod.invoke(null,new Object[]{new String[]{new String("123"),new String("abc"),new String("xyz")}});

int [] a1 = new int[]{1,2,3};
int [] a2 = new int[4];
int[][] a3 = new int[2][3];
String[] a4 = new String[]{"a","b","c"};
System.out.println(a1.getClass() == a2.getClass());
System.out.println(a1.getClass().getName());
System.out.println(a1.getClass().getSuperclass().getName());
System.out.println(a1.getClass() == a4.getClass());
System.out.println(a3.getClass().getName());
System.out.println(a4.getClass().getSuperclass().getName());
System.out.println(a1.getClass() == a3.getClass());

//将数组遍历的方法Arrays.asList(),该类在utils包中
System.out.println(a1);
System.out.println(a4);
//asList()方法对int类型不转换
System.out.println(Arrays.asList(a1));
System.out.println(Arrays.asList(a4));

printObject(a4);
printObject("xyz");
}

/*
* 对象的属性一定要是公共的,否则无法操作该属性的值
*/
public static void changeStringValue(Object obj) throws Exception{
Field[] fields = obj.getClass().getFields();
System.out.println(fields.length);
for(Field field : fields){
//字节码只有一份,所以用==比较
if(field.getType() == String.class){
String odlStr = (String)field.get(obj);
String newValue = odlStr.replace('b', 'a');
field.set(obj, newValue);
}
}
}

public static void printObject(Object obj){
Class clazz = obj.getClass();
if(clazz.isArray()){
int len = Array.getLength(obj);
for(int i = 0; i < len; i++){
System.out.println(Array.get(obj, i));
}
}else{
System.out.println(obj);
}
}
}

class TestArguments{
public static void main(String[]args){
for(String arg : args){
System.out.println(arg);
}
}
}

 config.properties

className=java.util.ArrayList

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