getFields()与getDeclaredFields()区别

Java代码  收藏代码
  1. 1.package study.reflection;     
  2. 2.    
  3. 3.public class People {     
  4. 4.public String name = null;     
  5. 5.private String sex = null;     
  6. 6.private String age = null;     
  7. 7.private String tel = null;     
  8. 8.private String address = null;     
  9. 9.public static String s = null;     
  10. 10.static {     
  11. 11.   System.out.println("loading People");     
  12. 12.}     
  13. 13.    
  14. 14.public static void showPeople() {     
  15. 15.    
  16. 16.}     
  17. 17.    
  18. 18.public People(String name) {     
  19. 19.   this.name = name;     
  20. 20.}     
  21. 21.    
  22. 22.private People() {     
  23. 23.   this.name = name;     
  24. 24.}     
  25. 25.    
  26. 26.private void showPeopleInfo() {     
  27. 27.   System.out.println(name + " " + sex + " " + age + " " + tel + " "    
  28. 28.     + address);     
  29. 29.}     
  30. 30.    
  31. 31.public String getName() {     
  32. 32.   return name;     
  33. 33.}     
  34. 34.    
  35. 35.public void setName(String name) {     
  36. 36.   this.name = name;     
  37. 37.}     
  38. 38.    
  39. 39.public String getSex() {     
  40. 40.   return sex;     
  41. 41.}     
  42. 42.    
  43. 43.public void setSex(String sex) {     
  44. 44.   this.sex = sex;     
  45. 45.}     
  46. 46.    
  47. 47.public String getAge() {     
  48. 48.   return age;     
  49. 49.}     
  50. 50.    
  51. 51.public void setAge(String age) {     
  52. 52.   this.age = age;     
  53. 53.}     
  54. 54.    
  55. 55.public String getTel() {     
  56. 56.   return tel;     
  57. 57.}     
  58. 58.    
  59. 59.public void setTel(String tel) {     
  60. 60.   this.tel = tel;     
  61. 61.}     
  62. 62.    
  63. 63.public String getAddress() {     
  64. 64.   return address;     
  65. 65.}     
  66. 66.    
  67. 67.public void setAddress(String address) {     
  68. 68.   this.address = address;     
  69. 69.}     
  70. 70.    
  71. 71.}     
  72. 72.    
  73. 73.package esg;     
  74. 74.    
  75. 75.import java.lang.reflect.Constructor;     
  76. 76.import java.lang.reflect.Field;     
  77. 77.import java.lang.reflect.Method;     
  78. 78.    
  79. 79.import study.reflection.People;     
  80. 80.    
  81. 81.public class Esg {     
  82. 82.    
  83. 83.public static void main(String[] a) throws ClassNotFoundException {     
  84. 84.   Class c1 = People.class;     
  85. 85.    
  86. 86.   Field[] fs = c1.getFields();     
  87. 87.   System.out.println("*******getFields()*********");     
  88. 88.   for (int i = 0; i < fs.length; i++) {     
  89. 89.    System.out.println(fs[i].getName());     
  90. 90.   }     
  91. 91.   System.out.println("*******getDeclaredFields()*********");     
  92. 92.   fs = c1.getDeclaredFields();     
  93. 93.   for (int i = 0; i < fs.length; i++) {     
  94. 94.    System.out.println(fs[i].getName());     
  95. 95.   }     
  96. 96.   System.out.println("*******getMethods()*********");     
  97. 97.   Method[] md = c1.getMethods();     
  98. 98.   for (int i = 0; i < md.length; i++) {     
  99. 99.    System.out.println(md[i].getName());     
  100. 100.   }     
  101. 101.   System.out.println("*******getDeclaredMethods()*********");     
  102. 102.   md = c1.getDeclaredMethods();     
  103. 103.   for (int i = 0; i < md.length; i++) {     
  104. 104.    System.out.println(md[i].getName());     
  105. 105.   }     
  106. 106.    
  107. 107.   System.out.println("*******getConstructors()*********");     
  108. 108.   Constructor[] con = c1.getConstructors();     
  109. 109.   for (int i = 0; i < con.length; i++) {     
  110. 110.    System.out.println(con[i].getName());     
  111. 111.   }     
  112. 112.   System.out.println("*******getDeclaredConstructors()*********");     
  113. 113.   con = c1.getDeclaredConstructors();     
  114. 114.   for (int i = 0; i < con.length; i++) {     
  115. 115.    System.out.println(con[i].getName());     
  116. 116.   }     
  117. 117.}     
  118. 118.}     

 

getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问,能访问从其它类继承来的公有方法.getDeclaredFields()能访问类中所有的字段,与public,private,protect无关,不能访问从其它类继承来的方法  

getMethods()与getDeclaredMethods()区别:getMethods()只能访问类中声明为公有的方法,私有的方法它无法访问,能访问从其它类继承来的公有方法.getDeclaredFields()能访问类中所有的字段,与public,private,protect无关,不能访问从其它类继承来的方法  

getConstructors()与getDeclaredConstructors()区别:getConstructors()只能访问类中声明为public的构造函数.getDeclaredConstructors()能访问类中所有的构造函数,与public,private,protect无关 

posted @ 2015-07-23 15:11  Q_Quan  阅读(165)  评论(0)    收藏  举报