- 反射机制
- 简单使用反射机制解决问题
- Java反射机制可以完成
- 反射相关的主要类;
- 演示
- 反射机制优化
- 反射机制的优缺点
- 如何实现反射机制的优化
- Class类
- 基本介绍
- Class 的常用方法
- 代码演示
- 获取Class类对象的六种方法
- 哪些类有Class对象
总纲: 反射机制
package Reflection.question;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class ReflectionQuestion {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load (new FileReader("src\re.Properties"));
String classfullpath = properties.getProperty("classfullpath");
String method = properties.getProperty("method");
System.out.println("classfullpath="+classfullpath);
System.out.println("method="+method);
// new classfullpath();报错classfullpath是String类型的并不是想要的Cat类型
}
}
简单使用反射机制解决问题
package com.hspedu.reflection.question;
import com.hspedu.Cat;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
@SuppressWarnings({"all"})
public class ReflectionQuestion {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//根据配置文件 re.properties 指定信息, 创建Cat对象并调用方法hi
//老韩回忆
//传统的方式 new 对象 -》 调用方法
// Cat cat = new Cat();
// cat.hi(); ===> cat.cry() 修改源码.
//我们尝试做一做 -> 明白反射
//1. 使用Properties 类, 可以读写配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\re.properties"));
String classfullpath = properties.get("classfullpath").toString();//"com.hspedu.Cat"
String methodName = properties.get("method").toString();//"hi"
System.out.println("classfullpath=" + classfullpath);
System.out.println("method=" + methodName);
//2. 创建对象 , 传统的方法,行不通 =》 反射机制
//new classfullpath();
//3. 使用反射机制解决
//(1) 加载类, 返回Class类型的对象cls
Class cls = Class.forName(classfullpath);
//(2) 通过 cls 得到你加载的类 com.hspedu.Cat 的对象实例
Object o = cls.newInstance();
System.out.println("o的运行类型=" + o.getClass()); //运行类型
//(3) 通过 cls 得到你加载的类 com.hspedu.Cat 的 methodName"hi" 的方法对象
// 即:在反射中,可以把方法视为对象(万物皆对象)
Method method1 = cls.getMethod(methodName);
//(4) 通过method1 调用方法: 即通过方法对象来实现调用方法
System.out.println("=============================");
method1.invoke(o); //传统方法 对象.方法() , 反射机制 方法.invoke(对象)
}
}
1.在运行时判断任意一个对象所属的类
2在运行时构造任意一个类的对象
3.在运行时得到任意一个类所具有的成员变量和方法
4.在运行时调用任意一个对象的成员变量和方法
5.生成动态代理
1.java.lang.Class:代表一个类,Class对象表示某个类加载后在堆中的对象
2… java.lang.reflect.Method:代表类的方法,Method对象表示某个类的方法
3. java.lang.reflect.Field:代表类的成员变量, Field对象表示某个类的成员变量
4.java.lang.reflect.Constructor:代表类的构造方法,Constructor对象表示构造器
//java.lang.reflect.Field: 代表类的成员变量, Field对象表示某个类的成员变量
//得到name字段
//getField不能得到私有的属性
Field nameField = cls.getField("age"); //
System.out.println(nameField.get(o)); // 传统写法 对象.成员变量 , 反射 : 成员变量对象.get(对象)
//java.lang.reflect.Constructor: 代表类的构造方法, Constructor对象表示构造器
Constructor constructor = cls.getConstructor(); //()中可以指定构造器参数类型, 返回无参构造器
System.out.println(constructor);//Cat()
Constructor constructor2 = cls.getConstructor(String.class); //这里老师传入的 String.class 就是String类的Class对象
System.out.println(constructor2);//Cat(String name)
反射机制优化
反射机制的优缺点
如何实现反射机制的优化
Class类
基本介绍
Class 的常用方法
代码演示
package Reflection.Class;
import Reflection.Car;
import java.lang.reflect.Field;
public class Class02 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
//当前文件Class02所在的文件是Class ,目标文件是Car在当前文件的上一级Reflection中
String filefullname="Reflection.Car";
//获取到Class 类对象
//Class.forName(String classname)
Class> cls = Class.forName(filefullname);
//显示该cls对象是哪个类的Class对象
System.out.println(cls);
//显示cls的运行类型
System.out.println(cls.getClass());
System.out.println(cls.getName());
//通过反射创建对象
Car car = (Car)cls.newInstance();
System.out.println(car);
//通过反射获取属性brand
Field brand = cls.getField("brand");
System.out.println(brand.get(car));
//通过反射给属性赋值
brand.set(car,"五菱荣光");
System.out.println(brand);
//遍历Car的所有属性
Field[] fields = cls.getFields();
for(Field i:fields){
System.out.println(i.getName());
}
}
}
获取Class类对象的六种方法
代码:
public class GetClass_ {
public static void main(String[] args) throws ClassNotFoundException {
//1. Class.forName
String classAllPath = "com.hspedu.Car"; //通过读取配置文件获取
Class> cls1 = Class.forName(classAllPath);
System.out.println(cls1);
//2. 类名.class , 应用场景: 用于参数传递
Class cls2 = Car.class;
System.out.println(cls2);
//3. 对象.getClass(), 应用场景,有对象实例
Car car = new Car();
Class cls3 = car.getClass();
System.out.println(cls3);
//4. 通过类加载器【4种】来获取到类的Class对象
//(1)先得到类加载器 car
ClassLoader classLoader = car.getClass().getClassLoader();
//(2)通过类加载器得到Class对象
Class cls4 = classLoader.loadClass(classAllPath);
System.out.println(cls4);
//cls1 , cls2 , cls3 , cls4 其实是同一个对象
System.out.println(cls1.hashCode());
System.out.println(cls2.hashCode());
System.out.println(cls3.hashCode());
System.out.println(cls4.hashCode());
//后两种了解一下即可
//5. 基本数据(int, char,boolean,float,double,byte,long,short) 按如下方式得到Class类对象
Class integerClass = int.class;
Class characterClass = char.class;
Class booleanClass = boolean.class;
System.out.println(integerClass);//int
//6. 基本数据类型对应的包装类,可以通过 .TYPE 得到Class类对象
Class type1 = Integer.TYPE;
Class type2 = Character.TYPE; //其它包装类BOOLEAN, DOUBLE, LONG,BYTE等待
System.out.println(type1);
System.out.println(integerClass.hashCode());//?
System.out.println(type1.hashCode());//?
}
}
哪些类有Class对象



