Class类
对象照镜子后可以得到的信息:某个类的数据成员名、方法和构造器、某个类到底实现了哪些接口。
对于每个类而言,JRE 都为其保留一个不变的 Class 类型的对象。一个 Class 对象包含了特定某个类的有关信息。
Class 对象只能由系统建立对象
一个类在 JVM 中只会有一个Class实例
每个类的实例都会记得自己是由哪个 Class 实例所生成
获取 Class 对象的方式
- // @Test
- public void testClass() throws ClassNotFoundException{
- //直接通过类名 获取Class
- Class cls = UserInfo.class;
- //通过对象获取
- UserInfo u = new UserInfo();
- @SuppressWarnings("all")
- Class objCls = u.getClass();
- //通过类路径获取
- Class pathCls = Class.forName("reflectTest.UserInfo");
- }
Class类的常用方法
方法名 | 功能说明 |
static Class forName(String name) | 返回指定类名 name 的Class对象 |
Object newInstance() | 调用缺省构造函数,返回该Class对象的一个实例 |
Object newInstance(Object []args) | 调用缺省构造函数,返回该Class对象的一个实例 |
getName() | 返回此Class对象所表示的实体(类、接口、数组类、基本类型或void)名称 |
Class getSuperClass() | 返回当前Class对象的父类的Class对象 |
Class [] getInterfaces() | 获取当前Class对象的接口 |
ClassLoader getClassLoader() | 返回该类的类加载器 |
Class getSuperclass() | 返回表示此Class所表示的实体的超类的Class |
UserInfo
- <pre name="code" class="java">package reflectTest;
- public class UserInfo {
- private int id;
- private String name;
- private String sex;
- public UserInfo(){}
- private void add(){
- System.out.println(111);
- }
- private void add(int id){
- this.id = id;
- }
- public void add(int id, String name){
- System.out.println(222);
- }
- @Override
- public String toString() {
- return "UserInfo [id=" + id + ", name=" + name + ", sex=" + sex + "]";
- }
- }</pre>
- <pre></pre>
- <pre></pre>
通过Class对象获取类结构中的属性结构
- @Test
- public void testGetField() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
- Class cls = UserInfo.class;
- Field field = cls.getDeclaredField("id");
- //获取修饰符 0 default 1 public 2 private 4 protected
- System.out.print(field.getModifiers()+" ");
- //获取数据类型
- System.out.print(field.getType()+" ");
- //获取属性名称
- System.out.println(field.getName());
- }
通过Class对象获取到类中所有的属性集合
- @Test
- public void testGetAllField(){
- Class cls = UserInfo.class;
- Field[] fields = cls.getDeclaredFields();
- for (Field field : fields) {
- System.out.print(field.getModifiers()+" ");
- System.out.print(field.getType()+" ");
- System.out.println(field.getName());
- }
- Method[] methods = cls.getDeclaredMethods();
- for (Method method : methods) {
- System.out.print(method.getModifiers()+" ");
- System.out.print(method.getReturnType()+" ");
- System.out.print(method.getName()+" ");
- Class[] type = method.getParameterTypes();
- for (Class class1 : type) {
- System.out.print(class1+",");
- }
- System.out.println();
- }
- }
通过URLClassLoader 动态加载jar包中的类
- @Test
- public void testUrlClassLoader() throws MalformedURLException, ClassNotFoundException{
- File file = new File("src/reflectTest/dom4j-1.6.1.jar");
- URL url = file.toURI().toURL();
- URL[] urls = {url};
- URLClassLoader urlClassLoader = new URLClassLoader(urls);
- Class cls = urlClassLoader.loadClass("org.dom4j.Node");
- //获取所有的属性
- Field[] field = cls.getDeclaredFields();
- for (Field now : field) {
- System.out.print(now.getModifiers()+" ");
- System.out.print(now.getType()+" ");
- System.out.println(now.getName()+" ");
- }
- //获取所有的方法
- Method[] methods = cls.getDeclaredMethods();
- for (Method now : methods) {
- System.out.print(now.getModifiers()+" ");
- System.out.print(now.getReturnType()+" ");
- System.out.print(now.getName()+" ");
- Class[] types = now.getParameterTypes();
- System.out.print("(");
- for (Class type : types) {
- System.out.print(type+", ");
- }
- System.out.println(")");
- }
- }
无参数的构造方法的实例化
- <pre name="code" class="java">@Test
- public void constructorTest() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
- Class cls = Student.class;
- Constructor<Student> cons = cls.getDeclaredConstructor();
- Student stu = cons.newInstance();
- System.out.println(stu);
- }</pre>
- <pre></pre>
- <pre></pre>
有参数的构造方法的实例化
- @Test
- public void constructorParamTest() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
- Class cls = Student.class;
- Constructor<Student> cons = cls.getDeclaredConstructor(int.class, String.class);
- Student stu = cons.newInstance(100, "ZS");
- System.out.println(stu);
- }
- @Test
- public void constructorTest() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{
- //获取class对象
- Class cls = UserInfo.class;
- //获取构造方法
- Constructor<UserInfo> cons = cls.getDeclaredConstructor();
- //创建UserInfo对象
- UserInfo user = cons.newInstance();
- //获取到指定的属性
- Field field = cls.getDeclaredField("id");
- //私有的方法必须设置可以访问的权限
- field.setAccessible(true);
- //set方法可以设置某个对象的属性值
- field.set(user, 30);
- //get方法获取某个对象的属性值
- Object value = field.get(user);
- System.out.println(value);
- }
获取方法
- package reflectTest;
- import java.lang.reflect.Method;
- import org.junit.Test;
- /**
- * 情况1:判断某个类中是否存在某个属性或者方法。。。
- * 如果直接使用 出现编译错误
- * 情况2:如果想获取一个类中私有的方法和属性
- * 情况3:如果想获取一个类中所有的属性和方法
- * Class是反射的基础 通过这个类 可以获取类的结构
- * 类包含:
- * 属性(修饰符,类型,属性名称,属性值)
- * 获取属性方法
- * getDeclaredField("属性名“)
- * 可以获取当前类中(不包括父类)所有的属性(所有权限修饰符的属性)
- * getField("属性名")
- * 可以获取当前类或者他的你类中所有的public修饰的属性
- *
- * getDeclaredFields()
- * getFields()
- *
- * 方法(返回值 方法名称, 参数名称 参数类型)
- * getDeclaredMethod("方法名")
- * 可以获取当前类中(不包括你类)所有的方法(所有权限修饰符的方法)
- * getMethod("方法名")
- * 可以获取当前类或者他的父类中所有的public修饰的方法
- * getDeclaredMethods();
- * getMethods();
- * 构造方法(参数名称 参数类型)
- *
- * @author Administrator
- *
- */
- public class UserInfoFunTest {
- /**
- * 通过Class对象获取类结构跟方法结构
- * getDeclaredmethod(String 方法名)返回Method对象
- * @throws SecurityException
- * @throws NoSuchMethodException
- *
- */
- @Test
- public void testGetMethod() throws NoSuchMethodException, SecurityException{
- Class cls = UserInfo.class;
- Method method = cls.getDeclaredMethod("add", int.class, String.class);
- method.
- System.out.print(method.getModifiers()+" ");
- System.out.print(method.getReturnType()+" ");
- System.out.println(method.getName()+" ");
- Class[] clsType = method.getParameterTypes();
- for (Class class1 : clsType) {
- System.out.println("参数类型"+ class1);
- }
- }
- // @Test
- public void testGetAllMethod(){
- Class cls = UserInfo.class;
- Method[] methods = cls.getDeclaredMethods();
- for (Method method : methods) {
- System.out.print(method.getModifiers()+" ");
- System.out.print(method.getReturnType()+" ");
- System.out.print(method.getName()+" ");
- Class[] types = method.getParameterTypes();
- System.out.print("(");
- for (Class type : types) {
- System.out.print(type+" ");
- }
- System.out.println(")");
- }
- }
- }