يمكن لـ Java الحصول على أسماء متغيرات الأعضاء والمتغيرات الثابتة من خلال الانعكاس ، لذلك من غير المحتمل الحصول على المتغيرات المحلية.
اختبار الفئة العامة {public static void main (string [] args) يرمي الاستثناء {// todo method method method // احصل على قيم جميع المتغيرات clazz = class.forname ("com.qianmingxs.scoretable") ؛ الحقل [] الحقول = clazz.getfields () ؛ لـ (حقل الحقل: الحقول) {system.out.println (field.getName () + "" + field.getint (clazz)) ؛ } // احصل على قيمة متغير واحد/*clazz = class.forname ("com.qianmingxs.scoretable") ؛ حقل الحقل = clazz.getfield ("Five") ؛ system.out.println (field.getint (clazz)) ؛*/}} فئة Java لعرضها هي:
الفئة العامة scoreTable {private scoreTable () {} static static int int five = 100 ؛ // تصبح 5 نهائية عامة ثابتة int double_alive_four = 99 ؛ 96 ؛ // Life 4 Life 3 (لتحليل المعارضين) Final Static static int Alive_four_and_dead_three = 95 ؛ // Life 4 Dead 3 (لتحليل المعارضين) Final Final Static int Alive_four_and_alive_two = 94 ؛ // life 4 live 2 static int int inta_four = 93 ؛ Public Final Static int dead_four_and_alive_three = 91 ؛ // dead 4 live 3 live 3 public static int int dead_four_and_alive_two = 90 ؛ // dead 42 public final static int double_alive_three = 80 ؛ 65 ؛ // semi-living 4 (على غرار ○ ○اليا) ، تكون الأولوية أقل من 4 static int static int inta_three = 60 ؛ // living 3 public static int dead_four = 50 ؛ // dead 4 public static int double_alive_two = 40 ؛ dead_two = 10 ؛ // dead 2 static static int single = 0 ؛ // single}المثال البسيط أعلاه لاستخدام الانعكاس للحصول على أسماء متغيرة ثابتة وقيم متغيرة في فئات Java هو كل المحتوى الذي أشاركه معك. آمل أن تتمكن من إعطائك مرجعًا وآمل أن تتمكن من دعم wulin.com أكثر.