<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 創建測試類 ~~~ public class Person implements Serializable { private long id; public String name; public Person() { this.id = 100L; this.name = "jdxia"; } public Person(Long id, String name) { this.id = id; this.name = name; } public Person(long id) { this.id = id; } private Person(String name) { super(); this.name = name+"======="; System.out.println(name+"======="); } private String getSomeThing() { return "sdsadasdsasd"; } //getter/setter ~~~ # 反射 ~~~ package reflect; import com.sun.org.apache.xpath.internal.operations.String; import org.junit.Before; import org.junit.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class MyReflect { private java.lang.String className = null; private Class personClass = null; //反射Person類 @Before public void init() throws ClassNotFoundException { className = "reflect.Person"; //把這個類文件載入jvm的內存 personClass = Class.forName(className); } //獲取某個class文件對象 @Test public void getClassName() { System.out.println(personClass); } //獲取某個class文件對象的另一種方式 @Test public void getClassName2() { System.out.println(Person.class); } //創建一個class文件表示的實例對象,底層會調用空參數的構造方法 @Test public void getNewInstance() throws IllegalAccessException, InstantiationException { System.out.println(personClass.newInstance()); } //獲取非私有的構造函數 @Test public void getPublicConstructor() throws Exception { Constructor constructor = personClass.getConstructor(Long.class, String.class); Person person = (Person) constructor.newInstance(100L, "張三"); System.out.println(person.getId()); System.out.println(person.getName()); } //訪問私有的構造函數 @Test public void getPrivateConstructor() throws Exception { Constructor constructor = personClass.getDeclaredConstructor(String.class); //強制取消java的權限審查 constructor.setAccessible(true); Person person = (Person) constructor.newInstance("張三"); System.out.println("**" + person.getName()); } //訪問非私有成員變量 @Test public void getNotPrivateField() throws Exception { Constructor constructor = personClass.getConstructor(Long.class, String.class); Object obj = constructor.newInstance(100L, "zhangsan"); Field field = personClass.getField("name"); field.set(obj, "lisi"); System.out.println(field.get(obj)); } //訪問私有成員變量 @Test public void getPrivateMethod() throws Exception { //獲取空參構造函數 Object obj = personClass.newInstance(); Method method = personClass.getDeclaredMethod("getSomeThing"); method.setAccessible(true); Object value = method.invoke(obj); System.out.println(value); } @Test public void otherMethod() throws Exception { //當前加載這個class文件的那個類加載器對象 System.out.println(personClass.getClassLoader()); //獲取某個類實現的所有接口 Class[] interfaces = personClass.getInterfaces(); for (Class class1 : interfaces) { System.out.println(class1); } //反射當前這個類的直接父類 System.out.println(personClass.getGenericSuperclass()); /** * getResourceAsStream這個方法可以獲取到一個輸入流,這個輸入流會關聯到name所表示的那個文件上。 */ //path 不以’/'開頭時默認是從此類所在的包下取資源,以’/'開頭則是從ClassPath根下獲取。其只是通過path構造一個絕對路徑,最終還是由ClassLoader獲取資源。 System.out.println(personClass.getResourceAsStream("/log4j.properties")); System.out.println(personClass.getResourceAsStream("log4j.properties")); //判斷當前文件的class對象表示是否是數組 System.out.println(personClass.isArray()); System.out.println(new String[3].getClass().isArray()); //判斷當前的class對象表示是否是枚舉類 System.out.println(personClass.isEnum()); System.out.println(Class.forName("reflect.Person").isEnum()); System.out.println(personClass.isInterface()); System.out.println(Class.forName("reflect.Person").isInterface()); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看