反射的五大核心类:Class、Constructor、Method、Field、
一、反射与父类——Class
1.取得父类包名称 getPackage()
2.取得父类的Class对象 getSuperclass()
3.取得实现的所有分类接口 getInterfaces():返回值为数组
package Practise;
interface IFruit {}
interface IMessage{}
class CLS implements IFruit,IMessage{}
public class Test1
{
public static void main(String[] args) {
Class<?> cls=CLS.class;//取得Class类的对象
//1.取得包的名称
System.out.println(cls.getPackage().getName());//Practise
//2.取得父类名称
System.out.println(cls.getSuperclass().getName());//java.lang.Object
//3.取得实现的所有父类接口
Class<?>[] iClass=cls.getInterfaces();
for(Class<?> class1:iClass)
{
System.out.println(class1.getName());//Practise.IFruit
//Practise.IMessage
}
}
}
二、反射与构造方法——Constructor
1.取得指定参数的构造方法
getConstructor():返回值为
Constructor,根据方法名及参数取得指定构造方法,权限为public,若指定名称的方法,在本类中没有,就继续查找父类
getDeclaredConstructor()
:返回值为
Constructor,只在本类中查找指定名称及参数类型的构造方法,与权限无关
2.取得类中所有构造
getConstructors():
只能取得本类及父类中访问权限为public的所有构造方法
getDeclaredConstructors()
:
能取得本类中所有权限的所有构造方法
package Practise;
import java.lang.reflect.Constructor;
class Person{public Person() {}public Person(String name) {}public Person(String name,int age) {}}public class Test1{public static void main(String[] args) {Class<?> cls=Person.class;//取得类中的全部构造方法Constructor<?>[] constructors=cls.getConstructors();for(Constructor<?> constructor:constructors){ //public Practise.Person()System.out.println(constructor);//public Practise.Person(java.lang.String)} //public Practise.Person(java.lang.String,int)}}
3.对象实例化
newInstance():通过Constructor类创造对象实例化,根据类取得参数的实例化对象
package Practise;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class Person
{
private String name;
private int age;
public Person(String name,int age)
{
this.age=age;
this.name=name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Person [name="+name+",age="+age+"]";
}
}
public class Test1
{
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> cls=Person.class;
// 取得指定参数类型的构造方法对象
Constructor<?> constructor=cls.getConstructor(String.class,int.class);
System.out.println(constructor.newInstance("lxy",20));
}
}
三、
反射与普通方法
——Method
1.取得全部普通方法
getMethods():取得本类及父类中所有权限为public的普通方法
getDeclaredMethods():取得本类中的所有普通方法,无关权限
package Practise;
import java.lang.reflect.Method;
class Person
{
private String name;
private int age;
public Person(String name,int age)
{
this.age=age;
this.name=name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Person [name="+name+",age="+age+"]";
}
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 class Test1
{
public static void main(String[] args) {
Class<?> cls=Person.class;
Method[] methods=cls.getMethods();
for(Method method:methods)
{
System.out.println(method);
}
}
}
2.取得指定普通方法
getMethod():返回值为Method,根据方法名及参数取得指定普通方法,权限为public,若指定名称的方法,在本类中没有,就继续查找父类
getDeclaredMethod():返回值为Method,同上
getDeclaredConstructor()
,只不过,只在本类中查找指定名称及参数类型的普通方法,与权限无关
3.调用拿到普通方法
invoke():通过反射调用拿到Method方法
四、反射与类中属性——
Field
1.取得类中全部属性
getFields():同上
getMethods()
,先从本类中查找,在从父类中查找,权限为public,且全局变量final修饰的属性也可以找到
getDeclaredFields():同上getDeclaredConstructors(),只在本类中查找,与权限无关
2.取得类中指定名称属性
getField():返回值为Field,同上getMethod(),根据方法名及参数取得指定属性,权限为public,若指定名称的属性,在本类中没有,就继续查找父类
getDeclaredFields):返回值为Field,同上getDeclaredConstructor(),只不过,只在本类中查找指定名称的属性,与权限无关
3.取得属性类型
getType()4.动态破坏封装性
setAccessible():不是Field单独具备的,Method和Constructor也有
五、反射与注解
1.取得全部注解:getAnnotations(),返回值为Annotation[]数组
package reflect; import java.io.Serializable; import java.lang.annotation.Annotation; //取得定义在类上的注解 @SuppressWarnings("serial") @Deprecated class Member implements Serializable{} public class Test1 { public static void main(String[] args) { Annotation[] annotations = Member.class.getAnnotations(); for(Annotation a:annotations) { System.out.println(a); } } }
public class Test1 { public static void main(String[] args) { Annotation[] annotations =new Annotation[0]; try { annotations = Member.class.getMethod("toString").getAnnotations(); } catch (NoSuchMethodException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(Annotation a:annotations) { System.out.println(a); } } }
package reflect;
import java.io.Serializable;
import java.lang.annotation.Annotation;
//取得定义在方法上的注解
@SuppressWarnings("serial")
@Deprecated
class Member implements Serializable
{
@Deprecated
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
public class Test1
{
public static void main(String[] args) {
Annotation[] annotations =new Annotation[0];
try {
annotations = Member.class.getMethod("toString").getAnnotations();
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Annotation a:annotations)
{
System.out.println(a);
}
}
}
2.取得指定的注解:getAnnotation
3.注解的三大作用范围
(1)SOURCE:表示此注解只在源代码阶段起作用
(2)CLASS:表示此注解在*.class文件中起作用(包含源文件 *.java)
(3)RUNTIME:表示此注解在JVM运行阶段依然起作用
(2)CLASS:表示此注解在*.class文件中起作用(包含源文件 *.java)
(3)RUNTIME:表示此注解在JVM运行阶段依然起作用