public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private long id;
private String name = "Mike";
private int age = 20;
/**私有构造器也能通过反射访问*/
private Customer(){
}
public Customer(String name,int age){
this.name = name;
this.age = 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;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Customer other = (Customer) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString(){
return this.name+": "+this.age;
}
protected void finalize(){
System.out.println(name + " is collected.");
}
}
public class ReflectDemo {
public static void main(String[] args) {
Class<?> myclass;
try {
myclass = Class.forName("demo.reflect.Customer");
useClass(myclass);
useConstructors(myclass);
getFields(myclass);
getMethods(myclass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**使用Class创建对象*/
static void useClass(Class<?> myclass) {
try {
Customer customer = (Customer) myclass.newInstance();
customer.setName("xiong");
customer.setAge(24);
System.out.println(customer);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("----Class's newInstance method can not access private Constructor.----");
}
}
/**使用Constructor创建对象*/
static void useConstructors(Class<?> myclass) {
Constructor<?>[] cons = myclass.getDeclaredConstructors();
for (Constructor<?> con : cons) {
/**动态设置构造器的访问权限,此时可以访问private的构造器*/
con.setAccessible(true);
try {
if (con.getParameterTypes().length == 0) {
Customer customer1 = (Customer) con
.newInstance(new Object[] {});
System.out.println(customer1);
}
if (con.getParameterTypes().length == 2) {
Customer customer2 = (Customer) con
.newInstance(new Object[] { new String("xiong"),
new Integer(24) });
System.out.println(customer2);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**动态更新对象的属性值*/
static void getFields(Class<?> myclass) {
Customer customer = new Customer("Haha",40);
Field[] fields = myclass.getDeclaredFields();
for(Field field:fields){
if(field.getName().equals("age")){
try {
/**动态设置属性的访问权限,此时可以访问private的属性*/
field.setAccessible(true);
field.set(customer, 14);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
System.out.println(customer);
}
/**获取该类的所有方法*/
static void getMethods(Class<?> myclass){
Method[] methods = myclass.getDeclaredMethods();
for(Method method:methods){
System.out.println(method);
}
}
}
分享到:
相关推荐
在《Java Reflection in Action》这本书中,作者深入探讨了这一主题,帮助开发者理解并有效地利用Java反射机制。这本书是2005年出版的英文版,对于想要提升Java编程技能,特别是对动态类型和元编程感兴趣的开发者来...
Java Reflection in Action这本书作为Java反射机制的经典之作,详细介绍了反射技术的应用和相关的概念基础,是Java开发者的必备书籍。 首先,反射允许开发者在不知道对象具体类型的情况下,通过类名获取类类型,...
本书《Java Reflection in Action》深入浅出地讲解了Java反射技术的各种应用场景和技术细节。书中通过一系列生动的例子展示了如何安全有效地使用反射技术解决实际问题。例如: 1. **使用反射进行动态代理**:介绍了...
《Java Reflection in Action》这本书深入探讨了这一主题,为开发者提供了理解和运用反射的强大工具。这本书包含了PDF版的全文以及示例源代码,帮助读者通过实践来学习。 1. **反射基础**: - 反射API:`java.lang...
《Java Reflection In Action》这本书深入探讨了这个主题,对于理解和掌握Java动态性有着极大的帮助。以下是关于Java反射的一些关键知识点: 1. **反射基础**:反射API主要包括`java.lang.Class`、`java.lang....
Java Reflection是Java提供的用于在运行时获取类、接口、方法、字段等信息的机制。在本论文中,作者利用反射机制开发了一个能够自动逆向分析Java源代码或编译后的.class文件的解析器。反射在动态语言编程中有着广泛...
Java Reflection in Action Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time providing the sound conceptual basis...
Java 反射(Reflection)是Java编程语言的一个重要特性,它允许程序在运行时动态地获取类的信息(如类名、方法、字段等)并直接操作对象的内部属性。这为开发者提供了强大的灵活性,能够在不预先知道具体类的情况下...
《JAVA反射在行动》这本书由Ira R. Forman和Nate Forman撰写,由MANNING出版社出版,是一本深入探讨Java反射机制及其应用的权威指南。反射是Java语言的一项强大特性,允许运行时检查类的信息并操作类的对象,这在很...
### Java反射机制详解 #### 一、Java反射机制概述 反射是Java编程语言的一个关键特性,它赋予了Java程序在运行时自我检查的能力,并能够直接操纵程序内部的属性。通过反射,开发者可以在运行时获取类的信息(如...
"基于Java Reflection的数据库访问通用模型设计与实现" 本文提出了基于Java Reflection机制和面向接口的编程思想,设计并实现了一款Java数据库访问通用模型,该模型具有适用范围广、轻量级、可扩展性好等特点,可以...
《Java Reflection in Action》一书由Ira R. Forman和Nate Forman共同撰写,旨在帮助读者深入理解Java反射机制的核心概念和技术细节,并通过一系列实用案例展示如何有效地利用反射技术来解决实际问题。 #### 二、...
### Java反射经典实例详解 #### 一、引言 反射是Java编程语言中的一个重要特性,它允许程序在运行时检查自身结构(如类、字段、方法等),并且可以在运行时调用任意一个类的成员(包括私有成员)。这一特性为Java...
Java Reflection (JAVA反射.mht