JavaSE8基礎 Class 擷取與設定非靜態私人成員變數

來源:互聯網
上載者:User

標籤:lang   main   pack   logs   throw   private   exception   release   import   

禮悟:
           好好學習多思考,尊師重道存感恩。葉見尋根三返一,活水清源藏於零。
           虛懷若穀良心主,皓月當空自在王。願給最苦行無悔,誠勸且行且珍惜。

 

    os :windows7 x64
    jdk:jdk-8u131-windows-x64
    ide:Eclipse Oxygen Release (4.7.0)



被解析類的代碼:

package blog.jizuiku2;/** *  *  * @author jizuiku * @version V17.10.02 */public class Person {// 成員變數public    int age;private   String password;protected String name;  int score;public    static   int ageStatic=13;    private   static String passwordStatic="hello world";protected static String nameStatic;  static int scoreStatic;  // 構造方法public    Person(int age) {System.out.println("public GZ:int");}public    Person(int age,int score) {System.out.println("public GZ:int,int");}private   Person() {System.out.println("private GZ:");}protected Person(String name) {System.out.println("protected GZ:String");}  Person(int age,String name){this.age=age;this.name=name;System.out.println("default GZ:int,String");}// 成員方法  public void sayHello(String name) {}private int sayHello() {return 1;}protected String sayHello(int age) {return "";}  voidsayHello(int age,String name) {}  public void sayHelloStatic(String name) {}private int sayHelloStatic() {return 1;}protected String sayHelloStatic(int age) {return "";}  voidsayHelloStatic(int age,String name) {}@Overridepublic String toString() {return "Person [age=" + age + ", password=" + password + ", name=" + name + ", score=" + score + "]";}}

 
示範類:

package blog.jizuiku2;import java.lang.reflect.Constructor;import java.lang.reflect.Field;/** * Class 非靜態成員變數的擷取與設定 *  * @author jizuiku * @version V17.10.02 */public class ClassGetNormalFieldDemo {public static void main(String[] args) throws Exception {String className = "blog.jizuiku2.Person";Class c = Class.forName(className);// 擷取所有的成員變數System.out.println("該類中所有的成員變數如下:");Field[] fs = c.getDeclaredFields();for (Field field : fs) {System.out.println(field);}System.out.println();// 擷取所有的構造方法System.out.println("該類中所有的建構函式如下:");Constructor[] cons = c.getDeclaredConstructors();for (Constructor constructor : cons) {System.out.println(constructor);}System.out.println();// 使用 blog.jizuiku0.Person(int,java.lang.String) 建構函式Constructor con = c.getDeclaredConstructor(int.class,java.lang.String.class);// 設定存取權限con.setAccessible(true);Object obj = con.newInstance(1,"hello");System.out.println((blog.jizuiku2.Person)obj);// 擷取某一成員變數 並 對其賦值Field passwordField = c.getDeclaredField("password");// 設定為可以訪問passwordField.setAccessible(true);// 對obj對象的password進行賦值passwordField.set(obj, "javaer");System.out.println(passwordField.get(obj));}}

 
結果:



Java優秀,值得學習。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

JavaSE8基礎 Class 擷取與設定非靜態私人成員變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.