1.什么是反射?
反射它是框架设计的灵魂。什么是框架: 它是一个别人设计好的一个半成品,在使用时只要填入自己的业务代码。好处: 提高开发效率。
反射: 在运行时,它是把类中成员抽取为其他类对象的过程。
2获取反射Class
任意的字节码都会通过ClassLoader类加载器,加载到JVM内存当中,并以Class对象的形式存在。
且每一个类的字节码只会加载一次
1.以路径加载Class对象 [Class。formName(”类路径“)]
2.用类名.class
3.对象getClass();
public class Test {
public static void main(String[] args) throws Exception {
//第一种方法通过teacher类路径获取teacher的反射类对象
Class aClass = Class.forName("day0810.demo01.Teacher");
//第二种方法根据类名获取teacher类的反射对象
Class teacherclass = Teacher.class;
//第三种
Teacher teacher = new Teacher();
Class aClass1 = teacher.getClass();
System.out.println(aClass==teacherclass);
System.out.println(aClass==aClass1);
}
}
class student{
}
class Teacher {
private String name;
private Integer age;
public String getName() {
return name;
}
3.怎样通过反射类型获取对象
用Class类对象,调用newInstance()
public class Test {
public static void main(String[] args) throws Exception {
//根据Teacher类路径获取对象
Class<Teacher> teacherClass = (Class<Teacher>) Class.forName("day0810.demo01.Teacher");
//根据反射类型找到对应的类对象
//之前通过new Teacher来获取类对象
Teacher teacher= teacherClass.newInstance();
}
}
4.获取反射类中的属性成员对象
getDeclaredFields() 返回的数组 |
getDeclaredField(String name) 返回一个 |
getField(String name) 返回一个 |
getFields() 返回包含一个数组 |
public class Test {
public static void main(String[] args) throws Exception {
//获取Lawyer 类的反射类
Class<Lawyer> lawyerClass = Lawyer.class;
//获取类中的成员对象 获取本类中的所有属性对象
Field[] declaredFields = lawyerClass.getDeclaredFields();
for(Field field: declaredFields){
System.out.println(field);
}
System.out.println("·············换行");
//获取本类中及父类的所有对象
Field[] fields= lawyerClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
Field profession=lawyerClass.getField("profession");
System.out.println(profession);
}
}
class Animal{
public String people;
public String profession;
}
class Lawyer extends Animal{
public String name;
private Integer age;
private Integer height;
String sex;
@Override
public String toString() {
return "Lawyer{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
4.2 Field类中具有的常用方法
set(Object obj , Object value) 指定对象的属性赋值
setAccessible(true): 可允许访问私有属性
getAnnotation(注解.class ): 获取属性里的注解对象
public class Test {
public static void main(String[] args) throws Exception{
//获取反射类对象
Class<Lawyer> lawyerClass = Lawyer.class;
//在用反射类得到类对象
Lawyer lawyer = lawyerClass.newInstance();
System.out.println(lawyer);
//获取到反射类中的age属性对象 再对,对象进行赋值
Field age = lawyerClass.getDeclaredField("age");
age.set(lawyer,22);
System.out.println(lawyer);
Field name = lawyerClass.getDeclaredField("name");
//setAccessible(true)可访问私有属性
name.setAccessible(true);
name.set(lawyer,"wyf");
System.out.println(lawyer);
//获取属性的注解对象
My annotation = name.getAnnotation(My.class);
System.out.println(annotation.value());
}
@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface My{
String value();
}
}
class Lawyer{
@Test.My(value="姓名")
private String name;
public Integer age;
@Override
public String toString() {
return "Lawyer{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
5.获取方法类的对象
- getDeclaredMethods 返回的数组对于每个此类方法的
方法
对象。 - getDeclaredMethod(String name, 类<?>... parameterTypes) 获取本类指定方法
- getMethods() 返回包含一个数组 方法对象反射由此表示的类或接口的所有公共方法类对象
- getMethod(String name, 类<?>... parameterTypes) 返回一个方法对象,它反映表示的类或接口的指定公共成员方法类对象
public class Test {
public static void main(String[] args) throws Exception{
Class<Animal> animalClass = Animal.class;
Animal animal = animalClass.newInstance();
//获取此类中的 所有Methods方法对象
Method[] declaredMethods = animalClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
//获取此类及父类中public修饰的 方法对象
Method[] methods = animalClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
Method dog = animalClass.getMethod("dog", String.class);
System.out.println(dog);
}
}
class Animal{
public String dog(){
System.out.println("狗");
return "dog";
}
public String dog(String name){
System.out.println("狗");
return "dog";
}
public String cat(Integer age){
System.out.println("猫");
return "cat";
}
public String duck(){
System.out.println("鸭");
return "duck";
}
}
5.2Method类中常用的方法
invoke(): 执行该方法体。
getAnnotation()
public class Test1 {
public static void main(String[] args)throws Exception {
Class<people> peopleClass = people.class;
people people = peopleClass.newInstance();
Method wyf = peopleClass.getMethod("lyf", int.class, String.class);
Object lyf = wyf.invoke(people, 22, "lyf");
System.out.println(lyf);
My annotation = wyf.getAnnotation(My.class);
System.out.println(annotation.value());
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface My{
String value();
}
class people{
public String wyf(){
System.out.println("踩缝纫机中......");
return "wyf";
}
@My("塌房了")
public String lyf(int age,String name){
System.out.println("年龄"+age+";姓名"+name);
return "lyf";
}
public String ggg(int age){
System.out.println("111111");
return "wyf"+age;
}
}
6. 获取构造对象
getConstructor(类<?>... parameterTypes)
返回一个 Constructor
对象,该对象反映 Constructor
对象表示的类的指定的公共 类
函数。
返回包含一个数组 Constructor
对象反射由此表示的类的所有公共构造 类
对象。
返回一个反映 Constructor
对象表示的类声明的所有 Constructor
对象的数组 类
getDeclaredConstructor(类<?>... parameterTypes)
返回一个 Constructor
对象,该对象反映 Constructor
对象表示的类或接口的指定 类
函数。
public class Test2 {
public static void main(String[] args)throws Exception {
Class<?> aClass = Class.forName("day0810.demo06.YYY");
//调用构造函数
Object o = aClass.newInstance();
Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
//可得到本类的
Constructor<?>[] constructors = aClass.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
}
}
class AAA{
public AAA(){
System.out.println("父类的无参构造");
}
public AAA(String name){
System.out.println("这是父类的有参构造"+name);
}
}
class YYY extends AAA{
public YYY(){
super();
System.out.println("子类的无参构造");
}
public YYY(String name){
System.out.println("子类的有参:"+name);
}
private YYY(Integer age){
System.out.println("有参私有构造");
}
}
6.2Constructor类中常用的方法
public class Test2 {
public static void main(String[] args)throws Exception {
Class<?> aClass = Class.forName("day0810.demo06.YYY");
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
Object o = declaredConstructor.newInstance();
Constructor<?> declaredConstructor1 = aClass.getDeclaredConstructor(String.class);
Object world = declaredConstructor1.newInstance("hello world");
}
}
class AAA{
public AAA(){
System.out.println("父类的无参构造");
}
public AAA(String name){
System.out.println("这是父类的有参构造"+name);
}
}
class YYY extends AAA{
public YYY(){
super();
System.out.println("子类的无参构造");
}
public YYY(String name){
System.out.println("子类的有参:"+name);
}
private YYY(Integer age){
System.out.println("有参私有构造");
}
}