ava中的类可以抽取共性内容。描述类的类。就是Class,
其中常见共性属性类有:Constructor,Field,Method,Modifier.Array。
Class对象是基本类型。数组。类的字节码;
获取Class对象有3种方法:
1,类名.Class
2,引用.getClass();
3,Class.forName((String)name);(带包名)
通过Class的get方法可以获得其中的属性也有对属性进行判断的方法。并且通过属性类的方法来建立对象.
其中私有的属性可以用getDeclaredXXX();强行获取;且用
setAccessible
(boolean flag)进行设置标记
Constructor con=String.class.getConstructor(Class<?>...parameterTypes)
Constructor con=String.class.getConstructor(Class<?>...parameterTypes)
建立有参构造函数的对象: con.newInstance(实际参数);
无参的建立方法为Class对象里的newInstance();
Field field =String.class.getField((String)成员变量名);
获取值要用Object obj = field.get(String);
Method method=String.class.getMethod((String)name, Class<?>...parameterTypes)
为了兼容以前版本。Object数组作为参数时会自动拆包。
调用函数:method.invoke(对象引用名(静态为null),参数(方法的实际参数));
拿到方法。然后作用于某个对象。
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
public class ReflectText {
public static void main(String[] args)throws Exception{
Constructor ct = Class.forName("java.lang.String").getConstructor(StringBuffer.class);
String str = (String)ct.newInstance(new StringBuffer("hehe"));
System.out.println(str);
ReflectPiont rp = new ReflectPiont(3,5);
Field fieldy=rp.getClass().getField("y");
int n =(int)fieldy.get(rp);
System.out.println(n);
Field fieldx=rp.getClass().getDeclaredField("x");
fieldx.setAccessible(true);
System.out.println(fieldx.get(rp));
changeString(rp);
System.out.println(rp);
String name =args[0];
Method me =Class.forName(name).getMethod("main", String[].class);
me.invoke(null, (Object)new String[]{"11","22","33"});
}
private static void changeString(Object obj)throws Exception{
Field[] fields = obj.getClass().getFields();
for(Field field:fields){
if(field.getType()==String.class){
String str=(String)field.get(obj);
String str1=str.replace("b", "a");
field.set(obj, str1);
}
}
import java.util.Date;
public class ReflectPiont {
//产生源代码自动生产hashCode和equals
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPiont other = (ReflectPiont) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public Piont d = new Piont();
public Piont getD() {
return d;
}
public void setD(Piont d) {
this.d = d;
}
private int x;
public int 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;
}
public String str1="bool";
public String str2="base";
public ReflectPiont(int x, int y) {
super();
this.x = x;
this.y = y;
}
public String toString()
{
return str1+":"+str2;
}
}
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.beanutils.BeanUtils;
public class ReflectText2 {
public static void main(String[] args)throws Exception {
//InputStream is = new FileInputStream("src/javaenhance/cn/config.properties");
InputStream is = ReflectText2.class.getResourceAsStream("config.properties");
Properties pro = new Properties();
pro.load(is);
is.close();
String className= pro.getProperty("className");
Collection collection = (Collection)Class.forName(className).newInstance();
ReflectPiont rp1 = new ReflectPiont(3,5);
ReflectPiont rp2 = new ReflectPiont(3,5);
ReflectPiont rp3= new ReflectPiont(6,5);
ReflectPiont rp4 = new ReflectPiont(10,5);
collection.add(rp1);
collection.add(rp2);
collection.add(rp3);
collection.add(rp4);
System.out.println(collection.size());
System.out.println(BeanUtils.getProperty(rp1, "x"));
BeanUtils.setProperty(rp1, "d.y", "2000");
System.out.println(rp1.d.getY());
}
}
数组的字节码对象也为Class对象。
数组与Object的关系。
Object=int[ ]
Object[ ]=int[ ][ ]
Object[ ]=String[ ]
基本数据类型字节码8个。加一个void;
Arrays.aList();其中int[]里的元素 无法被分别存入集合。因为被视为一个对象。String[]则成功会被拆包,每一个元素都视为一个object对象。
如果因为某种原因,您并不确定参数或对象是不是数组,您可以检索对象的 Class 对象并询问它。 Class 类的 isArray() 方法将会告诉您。一旦您知道拥有了一个数组,您可以询问 Class 的 getComponentType() 方法,您实际拥有的是什么类型的数组。如果 isArray() 方法返回 false,那么 getComponentType() 方法返回空。否则返回元素的 Class 类型。如果数组是多维的,您可以递归调用 isArray() 。它将仍只包含一个 component 类型。此外,您可以用在 java.lang.reflect 包里找到的 Array 类的 getLength() 方法获取数组的长度。
public void recurse(Object arr)
{
if (arr.getClass().isArray())
{
for (int i=0;i<Array.getLength(arr);i++)
{
boolean check=false;
try{
check=Array.get(arr,i).getClass().isArray();
}catch(Exception e){}
if (check) recurse(Array.get(arr,i));
else sum+=Array.getInt(arr,i);
}
}
System.out.println(sum);
}