1.JAVA中无论生成一个类的多少个对象,这些对象都会对应同一个Class对象.
2.获取某个类或对象所对应的Class对象的常用的3种方式:
a.使用Class类的静态方法forName,Class.forName("java.lang.String");
b.使用.class语法:String.class;
c.使用对象的getClass()方法: String s = "aa"; Class> clazz = s.getClass();
3.若想通过类的不带参数的构造方法来生成对象,我们有两种方式:
a.先将获得Class对象,然后通过该Class对象的newInstance()方法直接生成即可:Class> classType = String.class;Object obj = classType.newInstance();
b.先获得Class对象,然后通过该对象获得对应的Constructor对象,
在通过该Constructor对象的newInstance()方法生成
-
package com.jelly.reflect;
-
-
import java.lang.reflect.Field;
-
import java.lang.reflect.InvocationTargetException;
-
import java.lang.reflect.Method;
-
-
-
-
-
-
-
-
public class ReflectTester {
-
-
-
-
-
-
-
-
public Object copy(Object object) throws Exception {
-
Class> classType = object.getClass();
-
Object obj = classType.getConstructor(new Class[] {}).newInstance(
-
new Object[] {});
-
Field[] fields = classType.getDeclaredFields();
-
for (Field field : fields) {
-
String name = field.getName();
-
String firstLetter = name.substring(0, 1).toUpperCase();
-
String getMethodName = "get" + firstLetter + name.substring(1);
-
String setMethodName = "set" + firstLetter + name.substring(1);
-
Method getMethod = classType.getMethod(getMethodName,
-
new Class[] {});
-
Method setMethod = classType.getMethod(setMethodName,
-
new Class[] { field.getType() });
-
Object value = getMethod.invoke(object, new Object[] {});
-
System.out.println(value);
-
setMethod.invoke(obj, new Object[] { value });
-
}
-
return obj;
-
}
-
-
public static void main(String[] args) throws Exception,
-
InvocationTargetException {
-
ReflectTester test = new ReflectTester();
-
Customer customer = new Customer("jelly", 23);
-
Customer obj = (Customer) test.copy(customer);
-
System.out.println(obj.getName() + obj.getAge());
-
}
-
}
-
-
class Customer {
-
public Customer() {
-
-
}
-
-
public Customer(String name, int age) {
-
this.name = name;
-
this.age = age;
-
}
-
-
private long id;
-
private String name;
-
private int age;
-
-
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;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
}
package com.jelly.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* JAVA反射技术
*
* @author Jelly QQ:136179492
*
*/
public class ReflectTester {
/**
* 通过一个对象利用反射技术将其复制
*
* @param 参数object
* @return 复制的Object对象
* @throws Exception
*/
public Object copy(Object object) throws Exception {
Class> classType = object.getClass();
Object obj = classType.getConstructor(new Class[] {}).newInstance(
new Object[] {});
Field[] fields = classType.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
String firstLetter = name.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + name.substring(1);
String setMethodName = "set" + firstLetter + name.substring(1);
Method getMethod = classType.getMethod(getMethodName,
new Class[] {});
Method setMethod = classType.getMethod(setMethodName,
new Class[] { field.getType() });
Object value = getMethod.invoke(object, new Object[] {});
System.out.println(value);
setMethod.invoke(obj, new Object[] { value });
}
return obj;
}
public static void main(String[] args) throws Exception,
InvocationTargetException {
ReflectTester test = new ReflectTester();
Customer customer = new Customer("jelly", 23);
Customer obj = (Customer) test.copy(customer);
System.out.println(obj.getName() + obj.getAge());
}
}
class Customer {
public Customer() {
}
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
private long id;
private String name;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}