* java.lang.Class:是反射的源头。
* 我们创建了一个类,通过编译(javac.exe),生成对应的.class文件。之后我们使用java.exe加载(JVM的类加载器完成的)
* 此.class文件,此.class文件加载到内存以后,就是一个运行时类,存在在缓存区。那么这个运行时类本身就是一个Class的实例!
* 1.每一个运行时类只加载一次!
* 2.有了Class的实例以后,我们才可以进行如下的操作:
* 1)*创建对应的运行时类的对象
* 2)获取对应的运行时类的完整结构(属性、方法、构造器、内部类、父类、所在的包、异常、注解、...)
* 3)*调用对应的运行时类的指定的结构(属性、方法、构造器)
* 4)反射的应用:动态代理
1 //在有反射以前,如何创建一个类的对象,并调用其中的方法、属性 2 @Test 3 public void test1(){ 4 Person p = new Person(); 5 // Person p1 = new Person(); 6 p.setAge(10); 7 p.setName("TangWei"); 8 System.out.println(p); 9 p.show(); 10 // p.display("HK"); 11 }
1 //有了反射,可以通过反射创建一个类的对象,并调用其中的结构 2 @Test 3 public void test2() throws Exception{ 4 Class clazz = Person.class; 5 6 // Class clazz1 = String.class; 7 8 //1.创建clazz对应的运行时类Person类的对象 9 Person p = (Person)clazz.newInstance(); 10 System.out.println(p); 11 //2.通过反射调用运行时类的指定的属性 12 //2.1 13 Field f1 = clazz.getField("name"); 14 f1.set(p,"LiuDeHua"); 15 System.out.println(p); 16 //2.2 17 Field f2 = clazz.getDeclaredField("age"); 18 f2.setAccessible(true); 19 f2.set(p, 20); 20 System.out.println(p); 21 22 //3.通过反射调用运行时类的指定的方法 23 Method m1 = clazz.getMethod("show"); 24 m1.invoke(p); 25 26 Method m2 = clazz.getMethod("display",String.class); 27 m2.invoke(p,"CHN"); 28 29 }
1 @Test 2 public void test3(){ 3 Person p = new Person(); 4 Class clazz = p.getClass();//通过运行时类的对象,调用其getClass(),返回其运行时类。 5 System.out.println(clazz); 6 }
1 //如何获取Class的实例(3种) 2 @Test 3 public void test4() throws ClassNotFoundException{ 4 //1.调用运行时类本身的.class属性 5 Class clazz1 = Person.class; 6 System.out.println(clazz1.getName()); 7 8 Class clazz2 = String.class; 9 System.out.println(clazz2.getName()); 10 11 //2.通过运行时类的对象获取 12 Person p = new Person(); 13 Class clazz3 = p.getClass(); 14 System.out.println(clazz3.getName()); 15 16 //3.通过Class的静态方法获取.通过此方式,体会一下,反射的动态性。 17 String className = "com.atguigu.java.Person"; 18 19 20 Class clazz4 = Class.forName(className); 21 // clazz4.newInstance(); 22 System.out.println(clazz4.getName()); 23 24 //4.(了解)通过类的加载器 25 ClassLoader classLoader = this.getClass().getClassLoader(); 26 Class clazz5 = classLoader.loadClass(className); 27 System.out.println(clazz5.getName()); 28 29 System.out.println(clazz1 == clazz3);//true 30 System.out.println(clazz1 == clazz4);//true 31 System.out.println(clazz1 == clazz5);//true 32 }
1 //关于类的加载器:ClassLoader 2 @Test 3 public void test5() throws Exception{ 4 ClassLoader loader1 = ClassLoader.getSystemClassLoader(); 5 System.out.println(loader1); 6 7 ClassLoader loader2 = loader1.getParent(); 8 System.out.println(loader2); 9 10 ClassLoader loader3 = loader2.getParent(); 11 System.out.println(loader3); 12 13 Class clazz1 = Person.class; 14 ClassLoader loader4 = clazz1.getClassLoader(); 15 System.out.println(loader4); 16 17 String className = "java.lang.String"; 18 Class clazz2 = Class.forName(className); 19 ClassLoader loader5 = clazz2.getClassLoader(); 20 System.out.println(loader5); 21 22 //掌握如下 23 //法一: 24 ClassLoader loader = this.getClass().getClassLoader(); 25 InputStream is = loader.getResourceAsStream("com\\atguigu\\java\\jdbc.properties"); 26 //法二: 27 // FileInputStream is = new FileInputStream(new File("jdbc1.properties")); 28 29 Properties pros = new Properties(); 30 pros.load(is); 31 String name = pros.getProperty("user"); 32 System.out.println(name); 33 34 String password = pros.getProperty("password"); 35 System.out.println(password); 36 37 }
1 class Person extends Creature<String> implements Comparable,MyInterface{ 2 public String name; 3 private int age; 4 int id; 5 //创建类时,尽量保留一个空参的构造器。 6 public Person() { 7 super(); 8 // System.out.println("今天天气很闷热"); 9 } 10 public Person(String name) { 11 super(); 12 this.name = name; 13 } 14 private Person(String name, int age) { 15 super(); 16 this.name = name; 17 this.age = age; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public int getAge() { 26 return age; 27 } 28 public void setAge(int age) { 29 this.age = age; 30 } 31 32 public int getId() { 33 return id; 34 } 35 public void setId(int id) { 36 this.id = id; 37 } 38 @MyAnnotation(value = "abc123") 39 public void show(){ 40 System.out.println("我是一个人!"); 41 } 42 43 private Integer display(String nation,Integer i) throws Exception{ 44 System.out.println("我的国籍是:" + nation); 45 return i; 46 } 47 @Override 48 public String toString() { 49 return "Person [name=" + name + ", age=" + age + "]"; 50 } 51 @Override 52 public int compareTo(Object o) { 53 // TODO Auto-generated method stub 54 return 0; 55 } 56 57 public static void info(){ 58 System.out.println("中国人!"); 59 } 60 61 class Bird{ 62 63 } 64 65 }
1 import static java.lang.annotation.ElementType.CONSTRUCTOR; 2 import static java.lang.annotation.ElementType.FIELD; 3 import static java.lang.annotation.ElementType.LOCAL_VARIABLE; 4 import static java.lang.annotation.ElementType.METHOD; 5 import static java.lang.annotation.ElementType.PARAMETER; 6 import static java.lang.annotation.ElementType.TYPE; 7 8 import java.lang.annotation.Retention; 9 import java.lang.annotation.RetentionPolicy; 10 import java.lang.annotation.Target; 11 12 @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) 13 @Retention(RetentionPolicy.RUNTIME) 14 public @interface MyAnnotation { 15 String value(); 16 }
1 public class Creature<T>{ 2 public double weight; 3 4 public void breath(){ 5 System.out.println("呼吸!"); 6 } 7 }
1 import java.io.Serializable; 2 3 public interface MyInterface extends Serializable{ 4 5 }