1.(1)什么是class对象?
2.创建一个对象的过程?
我们在编写完java代码后会交给javac进行编译生成.class文件,当我们在某个java代码中调用这个对象时,java会调用ClassLoader类加载器,将这个class文件加载成为一个Class类对象,这个对象中的成员变量,构造方法,成员方法都是用数组将他们装起来,(编译器提示方法代码大多就是这个原理),然后会创建成为一个实例对象。
3.获取Class对象的方式?
(1)Class.forName(“全类名”):将字节码文件加载进内存,返回Class对象。
多用于配置文件,将类名定义在配置文件中,读取文件,加载类。
(2)类名.class:通过类名的属性class获取。
多用于参数的传递。
(3)对象.getClass():getclass方法在object类中定义。
多用与对象的获取字节码的方式。
同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,无论通过哪一种方式获取的class对象都是一个。
1 package cn; 2 3 import cn.entity.Persion; 4 5 public class calss09 { 6 public static void main(String[] args) throws ClassNotFoundException { 7 Class aClass = Class.forName("cn.entity.Persion"); 8 Class bClass = Persion.class; 9 Persion persion = new Persion(); 10 Class cClass = persion.getClass(); 11 Persion persion1= new Persion(); 12 Class dClass = persion.getClass(); 13 persion1.getClass(); 14 System.out.println(persion==persion1); 15 System.out.println(aClass==bClass); 16 System.out.println(aClass==dClass); 17 System.out.println(aClass==cClass); 18 System.out.println(cClass==dClass); 19 } 20 }
4.Class对象的方法?
1 package cn.entity; 2 3 public class Persion { 4 public String name; 5 private int age; 6 7 public Persion(String name, int age) { 8 this.name = name; 9 this.age = age; 10 } 11 12 public Persion() { 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Persion{" + 34 "name=‘" + name + ‘\‘‘ + 35 ", age=" + age + 36 ‘}‘; 37 } 38 39 public void eat(){ 40 System.out.println("eat...."); 41 } 42 public void hello(String a){ 43 System.out.println("say"+ a); 44 } 45 }
在反射面前,没有什么私有共有之分。
1 package cn; 2 3 import cn.entity.Persion; 4 5 import java.lang.reflect.Field; 6 7 public class calss10 { 8 public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { 9 Class p = Persion.class; 10 Field[] fields = p.getFields(); 11 for (Field file:fields) { 12 System.out.println(file); 13 } 14 System.out.println("-------------------------"); 15 Field field = p.getField("name"); 16 Persion persion = new Persion(); 17 Object name = field.get(persion); 18 System.out.println(name); 19 field.set(persion,"zhangsan"); 20 System.out.println(persion); 21 //获取所有成员变量,不考虑修饰符 22 Field[] declaredFields = p.getDeclaredFields(); 23 for (Field field1:declaredFields) { 24 System.out.println(field1); 25 } 26 System.out.println("-------------------------"); 27 Field field1 = p.getDeclaredField("age"); 28 field1.setAccessible(true);//暴力反射 29 Persion persion1 =new Persion(); 30 Object o = field1.get(persion1); 31 System.out.println(o); 32 } 33 }
运行结果:
构造方法
1 package cn; 2 3 import cn.entity.Persion; 4 5 import java.lang.reflect.Constructor; 6 import java.lang.reflect.InvocationTargetException; 7 8 public class calss11 { 9 public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { 10 //构造器 11 Class persionClass = Persion.class; 12 Constructor constructor = persionClass.getConstructor(String.class, int.class); 13 System.out.println(constructor); 14 Persion persion = new Persion(); 15 //用构造方法创建一个对象 16 Object asd = constructor.newInstance("asd", 10); 17 System.out.println(asd); 18 //空参构造方法创建对象 19 Object o = persionClass.newInstance(); 20 System.out.println(o); 21 } 22 }
运行结果:
方法:
1 package cn; 2 3 import cn.entity.Persion; 4 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.Method; 7 8 public class calss12 { 9 public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 10 //方法 11 Class<Persion> persionClass = Persion.class; 12 13 Method method = persionClass.getMethod("eat"); 14 Persion persion = new Persion(); 15 //执行方法 16 method.invoke(persion); 17 System.out.println("----------------"); 18 Method hello = persionClass.getMethod("hello", String.class); 19 hello.invoke(persion,"hello"); 20 } 21 }
做一个小反射框架
要求创建任意对象执行任意方法
(1)创建一个pro.properties
className=cn.it.entity.Persion
methodName=eat
(2)创建Persion类
1 package cn.it.entity; 2 3 public class Persion { 4 public String name; 5 private int age; 6 7 public Persion(String name, int age) { 8 this.name = name; 9 this.age = age; 10 } 11 12 public Persion() { 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Persion{" + 34 "name=‘" + name + ‘\‘‘ + 35 ", age=" + age + 36 ‘}‘; 37 } 38 39 public void eat(){ 40 System.out.println("eat...."); 41 } 42 public void hello(String a){ 43 System.out.println("say"+ a); 44 } 45 }
(3)反射
1 package cn.it.domain; 2 3 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.lang.reflect.InvocationTargetException; 7 import java.lang.reflect.Method; 8 import java.util.Properties; 9 10 public class calss13 { 11 public static void main(String[] args) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException { 12 //获取class目录下的配置文件 13 //加载配置文件,转化为一个集合 14 Properties properties= new Properties(); 15 ClassLoader classLoader = calss13.class.getClassLoader(); 16 InputStream resourceAsStream = classLoader.getResourceAsStream("cn/it/pro.properties"); 17 properties.load(resourceAsStream); 18 19 String className = properties.getProperty("className"); 20 String methodName = properties.getProperty("methodName"); 21 Class aClass = Class.forName(className); 22 Object o = aClass.newInstance(); 23 Method eat = aClass.getMethod("eat"); 24 eat.invoke(o); 25 26 } 27 }
中间写因为路径问题一直空指针,很郁闷所以把项目样子发一下
原文:https://www.cnblogs.com/fan123yh/p/11100170.html