1.认识反射机制
我们示例化的时候都是通过类名实例化对象,如果是通过对象找类名呢?
示例:
Person per = new Person(); // 实例化对象
System.out.println(per.getClass().getName());
2.认识Class类
在反射机制中,Class类是操作的源头,所有的反射操作从此类展开,实例化此类的方法:
-
通过Object类中的getClass方法
-
通过类.class方式
-
通过Class类的静态方法,forName方法.
示例:
public class ClassInstanceDemo01 {
public static void main(String[] args) {
//---1---
Person per = new Person(); // 实例化对象
Class<?> c = per.getClass(); // 为Class对象实例化
System.out.println(c.getName());
//---2----
Class<?> c = Person.class ;
System.out.println(c.getName());
//---3---
Class<?> c = null;
try {
c = Class.forName("com.ares.demo.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(c.getName());
}
}
}
三种方式中最常用的是第三种方式
.其次是
.class方法
.
3.反射机制实例化
3.1实例化无参构造的类
Person类
public class Person {
public String toString() {
return "hello world!!!";
}
}
--------------------------------------------------------
public class InstanceDemo{
public static void main(String[] args) {
Class<?> c = null;
try {
c = Class.forName("com.ares.demo.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Person p = (Person) c.newInstance(); //向下转型
System.out.println(p);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
注意:如果Person类中没有无参构造方法,继续反射调用就会报InstantiationException异常.
3.2实例化指定构造的类
如果没有无参构造,则必须通过制定的构造方法,并向其传递制定的参数才可以进行我们的实例化操作.
public Constructor<T> getConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
这个方法可以得到全部的构造
,因为一个类中可能存在多个构造方法
.所以会以数组形式返回
.
示例:
import java.lang.reflect.Constructor;
public class InstanceDemo {
public static void main(String[] args) throws Exception { // 所有异常抛出
Class<?> c = null;
Person per = null;
c = Class.forName("org.lxh.demo.classdemo03.Person");
Constructor<?> cons[] = c.getConstructors(); // 得到全部的构造
per = (Person) cons[0].newInstance("ARES", 24);// 实例化对象
System.out.println(per);
}
}
这种操作比起之前的无参反射显然要麻烦,所以在开发中,我们一般会保留一个无参构造方法.
4.通过Class类取得完整结构
-
取得父类 getSuperClass()
-
取得全部接口 getInterface()
-
取得全部构造 getConstructor()
注意的是用getName方法获得的只是类的名字,也就是构造的名字,但是其构造的参数,属性全都没有,要拼凑出一个构造方法则需要用到getModifiers()方法.
示例:
Person类
package com.ares.test;
interface Info {
public static final String AUTHOR = "ARES";
public String getInfo();
public String say(String name, String content);
public void sayHello();
}
public class Person implements Info {
private String name;
private int age;
public Person() {
}
public Person(String name) {
this.setName(name);
}
public Person(String name, int age) {
this(name);
this.setAge(age);
}
public String getInfo() {
return "Hello World!!!";
}
public String say(String name, String content) {
return name + "说了:" + content;
}
public void sayHello() {
System.out.println("hello --> " + AUTHOR);
}
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 String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}
GetConstructorDemo类:
import java.lang.reflect.Constructor;
public class GetConstructorDemo {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.ares.demo.Person");
Constructor<?> con[] = c.getConstructors(); // 得到全部构造
for (int i = 0; i < con.length; i++) {
int mod = con[i].getModifiers(); // 得到修饰符
Class<?> param[] = con[i].getParameterTypes(); // 得到全部的参数
System.out.print(mod + " "); // 输出内容
System.out.print(con[i].getName() + " (");
for (int x = 0; x < param.length; x++) {
System.out.print(param[x].getName() + " arg-" + x);
if (x < param.length - 1) {
System.out.print(",");
}
}
System.out.println(")");
}
}
}
输出
:
1 com.ares.test.Person ()
1 com.ares.test.Person (java.lang.String arg-0,int arg-1)
1 com.ares.test.Person (java.lang.String arg-0)
4.1 取得类中的全部方法
示例:
import java.lang.reflect.Method;
public class GetMethodDemo {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.ares.demo.Person");
Method m[] = c.getMethods(); // 取得全部的方法
for (int i = 0; i < m.length; i++) {
System.out.println(m[i]); // 输出内容
}
}
}
4.2 取得方法权限和返回值
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class GetMethodDemo02 {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.ares.demo.Person");
Method m[] = c.getMethods(); // 取得全部的方法
for (int i = 0; i < m.length; i++) {
String mod = Modifier.toString(m[i].getModifiers()); // 取得访问权限
String metName = m[i].getName(); // 取得方法名称
Class<?> ret = m[i].getReturnType();// 取得返回值类型
Class<?> param[] = m[i].getParameterTypes(); // 得到全部的参数类型
Class<?> exc[] = m[i].getExceptionTypes(); // 得到全部的异常
System.out.print(mod + " ");
System.out.print(ret + " ");
System.out.print(metName + " (");
for (int x = 0; x < param.length; x++) {
System.out.print(param[x] + "arg-" + x);
if (x < param.length - 1) {
System.out.print(",");
}
}
System.out.print(") ");
if (exc.length > 0) {// 有异常抛出
System.out.print("throws ");
for (int x = 0; x < exc.length; x++) {
System.out.print(exc[x].getName());
if (x < param.length - 1) {
System.out.print(",");
}
}
}
System.out.println();
}
}
}
4.3取得一个类的全部属性
示例:
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class GetFieldsDemo01 {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.ares.demo.Person");
{
Field f[] = c.getFields(); // 取得全部的变量
for (int i = 0; i < f.length; i++) {
String mod = Modifier.toString(f[i].getModifiers());
System.out.print(mod + " ");
System.out.println(f[i].getType().getName());
}
}
{
Field f[] = c.getDeclaredFields(); // 取得全部的变量
for (int i = 0; i < f.length; i++) {
String mod = Modifier.toString(f[i].getModifiers());
System.out.print(mod + " ");
System.out.println(f[i].getType().getName());
}
}
}
}
20150524
JAVA学习笔记系列
--------------------------------------------
联系方式
--------------------------------------------
Weibo: ARESXIONG
E-Mail: aresxdy@gmail.com
------------------------------------------------