• getFields()与getDeclaredFields()区别


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

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

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

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

  • 相关阅读:
    Python--初识函数
    Python中的文件操作
    Python中的集合
    Python中的编码和解码
    Python的关键字is和==
    Python中的字典
    Python中的列表和元组
    Python中几种数据的常用内置方法
    Python的编码
    python_while
  • 原文地址:https://www.cnblogs.com/chunxi/p/2229690.html
Copyright © 2020-2023  润新知