1.javabean
public class Person { private String name; public int age; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { this.name = name; this.age = age; } private Person(String name) { this.name = name; } public Person() { System.out.println("Person()"); } public void show(){ System.out.println("你好,我是一個人"); } private String showNation(String nation){ System.out.println("我的國籍是:" + nation); return nation; } }
2.
public class ReflectionTest { //反射之前,對於Person的操作 @Test public void test1() { //1.創建Person類的對象 Person p1 = new Person("Tom", 12); //2.通過對象,調用其內部的屬性、方法 p1.age = 10; System.out.println(p1.toString()); p1.show(); //在Person類外部,不可以通過Person類的對象調用其內部私有結構。 //比如:name、showNation()以及私有的構造器 } //反射之後,對於Person的操作 @Test public void test2() throws Exception{ Class clazz = Person.class; //1.通過反射,創建Person類的對象 Constructor cons = clazz.getConstructor(String.class,int.class); Object obj = cons.newInstance("Tom", 12); Person p = (Person) obj; System.out.println(p.toString()); //2.通過反射,調用對象指定的屬性、方法 //調用屬性 Field age = clazz.getDeclaredField("age"); age.set(p,10); System.out.println(p.toString()); //調用方法 Method show = clazz.getDeclaredMethod("show"); show.invoke(p); System.out.println("*******************************"); //通過反射,可以調用Person類的私有結構的。比如:私有的構造器、方法、屬性 //調用私有的構造器 Constructor cons1 = clazz.getDeclaredConstructor(String.class); cons1.setAccessible(true); Person p1 = (Person) cons1.newInstance("Jerry"); System.out.println(p1); //調用私有的屬性 Field name = clazz.getDeclaredField("name"); name.setAccessible(true); name.set(p1,"HanMeimei"); System.out.println(p1); //調用私有的方法 Method showNation = clazz.getDeclaredMethod("showNation", String.class); showNation.setAccessible(true); String nation = (String) showNation.invoke(p1,"中國");//相當於String nation = p1.showNation("中國") System.out.println(nation); } //疑問1:通過直接new的方式或反射的方式都可以調用公共的結構,開發中到底用那個? //建議:直接new的方式。 //什麼時候會使用:反射的方式。 反射的特徵:動態性 //疑問2:反射機制與面向對象中的封裝性是不是矛盾的?如何看待兩個技術? //不矛盾。 /* 關於java.lang.Class類的理解 1.類的加載過程: 程序經過javac.exe命令以後,會生成一個或多個字節碼文件(.class結尾)。 接着我們使用java.exe命令對某個字節碼文件進行解釋運行。相當於將某個字節碼文件 加載到內存中。此過程就稱爲類的加載。加載到內存中的類,我們就稱爲運行時類,此 運行時類,就作爲Class的一個實例。 2.換句話說,Class的實例就對應着一個運行時類。 3.加載到內存中的運行時類,會緩存一定的時間。在此時間之內,我們可以通過不同的方式 來獲取此運行時類。 */ //獲取Class的實例的方式(前三種方式需要掌握) @Test public void test3() throws ClassNotFoundException { //方式一:調用運行時類的屬性:.class Class clazz1 = Person.class; System.out.println(clazz1); //方式二:通過運行時類的對象,調用getClass() Person p1 = new Person(); Class clazz2 = p1.getClass(); System.out.println(clazz2); //方式三:調用Class的靜態方法:forName(String classPath) Class clazz3 = Class.forName("com.atguigu.java.Person"); // clazz3 = Class.forName("java.lang.String"); System.out.println(clazz3); System.out.println(clazz1 == clazz2); System.out.println(clazz1 == clazz3); //方式四:使用類的加載器:ClassLoader (瞭解) ClassLoader classLoader = ReflectionTest.class.getClassLoader(); Class clazz4 = classLoader.loadClass("com.atguigu.java.Person"); System.out.println(clazz4); System.out.println(clazz1 == clazz4); } //萬事萬物皆對象?對象.xxx,File,URL,反射,前端、數據庫操作 //Class實例可以是哪些結構的說明: @Test public void test4(){ Class c1 = Object.class; Class c2 = Comparable.class; Class c3 = String[].class; Class c4 = int[][].class; Class c5 = ElementType.class; Class c6 = Override.class; Class c7 = int.class; Class c8 = void.class; Class c9 = Class.class; int[] a = new int[10]; int[] b = new int[100]; Class c10 = a.getClass(); Class c11 = b.getClass(); // 只要數組的元素類型與維度一樣,就是同一個Class System.out.println(c10 == c11); } }
3.
public class NewInstanceTest { @Test public void test1() throws IllegalAccessException, InstantiationException { Class<Person> clazz = Person.class; /* newInstance():調用此方法,創建對應的運行時類的對象。內部調用了運行時類的空參的構造器。 要想此方法正常的創建運行時類的對象,要求: 1.運行時類必須提供空參的構造器 2.空參的構造器的訪問權限得夠。通常,設置爲public。 在javabean中要求提供一個public的空參構造器。原因: 1.便於通過反射,創建運行時類的對象 2.便於子類繼承此運行時類時,默認調用super()時,保證父類有此構造器 */ Person obj = clazz.newInstance(); System.out.println(obj); } //體會反射的動態性 @Test public void test2(){ for(int i = 0;i < 10;i++){ int num = new Random().nextInt(3);//0,1,2 String classPath = ""; switch(num){ case 0: classPath = "java.util.Date"; break; case 1: classPath = "java.lang.Object"; break; case 2: classPath = "com.atguigu.java.Person"; break; } try { Object obj = getInstance(classPath); System.out.println(obj); } catch (Exception e) { e.printStackTrace(); } } } /* 創建一個指定類的對象。 classPath:指定類的全類名 */ public Object getInstance(String classPath) throws Exception { Class clazz = Class.forName(classPath); return clazz.newInstance(); } }
4.
/** * 瞭解類的加載器 */ public class ClassLoaderTest { @Test public void test1(){ //對於自定義類,使用系統類加載器進行加載 ClassLoader classLoader = ClassLoaderTest.class.getClassLoader(); System.out.println(classLoader); //調用系統類加載器的getParent():獲取擴展類加載器 ClassLoader classLoader1 = classLoader.getParent(); System.out.println(classLoader1); //調用擴展類加載器的getParent():無法獲取引導類加載器 //引導類加載器主要負責加載java的核心類庫,無法加載自定義類的。 ClassLoader classLoader2 = classLoader1.getParent(); System.out.println(classLoader2); ClassLoader classLoader3 = String.class.getClassLoader(); System.out.println(classLoader3); } /* Properties:用來讀取配置文件。 */ @Test public void test2() throws Exception { Properties pros = new Properties(); //此時的文件默認在當前的module下。 //讀取配置文件的方式一: // FileInputStream fis = new FileInputStream("jdbc.properties"); FileInputStream fis = new FileInputStream("src\\jdbc.properties"); pros.load(fis); //讀取配置文件的方式二:使用ClassLoader //配置文件默認識別爲:當前module的src下 // ClassLoader classLoader = ClassLoaderTest.class.getClassLoader(); // InputStream is = classLoader.getResourceAsStream("jdbc1.properties"); // pros.load(is); String user = pros.getProperty("user"); String password = pros.getProperty("password"); System.out.println("user = " + user + ",password = " + password); } }