• Java基础学习10--反射


    1.获取类对象的三种方法

    首先定义一个用于测试的类Person:

     1 package com.example.reflection;
     2 
     3 public class Person {
     4     String name;
     5     int age;
     6 
     7     public Person() {
     8         super();
     9         System.out.println("Person的无参构造执行了");
    10     }
    11 
    12     public Person(String name, int age) {
    13         super();
    14         this.name = name;
    15         this.age = age;
    16     }
    17 
    18     public void eat() {
    19         System.out.println(name + "正在吃东西");
    20     }
    21 
    22     public String getName() {
    23         return name;
    24     }
    25 
    26     public void setName(String name) {
    27         this.name = name;
    28     }
    29 
    30     public int getAge() {
    31         return age;
    32     }
    33 
    34     public void setAge(int age) {
    35         this.age = age;
    36     }
    37 
    38     @Override
    39     public String toString() {
    40         return "Person [name=" + name + ", age=" + age + "]";
    41     }
    42 
    43     public void eat(String food) {
    44         System.out.println(name + "正在吃" + food);
    45     }
    46 
    47     private void prviMethod() {
    48         System.out.println("Person的私有方法");
    49     }
    50 
    51     static void staticMethod() {
    52         System.out.println("Person的静态方法");
    53     }
    54 }

    获取类对象有以下三种方式:

    (1)通过类的对象获取类对象

    (2)通过类名获取类对象

    (3)通过静态方法获取类对象

     1 package com.example.reflection;
     2 
     3 public class TestPerson {
     4     public static void main(String[] args) throws Exception {
     5         // 1.使用对象获取类对象
     6         Person p = new Person();
     7         Class<Person> cp = (Class<Person>) p.getClass();
     8         System.out.println(cp.hashCode());
     9 
    10         // 2.使用类名获取类对象
    11         Class<Person> cp2 = Person.class;
    12         System.out.println(cp2.hashCode());
    13 
    14         // 3.使用Class的formName()方法
    15         Class<Person> cp3 = (Class<Person>) Class.forName("com.example.reflection.Person");
    16         System.out.println(cp3.hashCode());
    17     }
    18 }

     三种方式获取的是同一个类对象。

    2.反射常用方法和操作

    反射常用方法有:

    public String getName()

    public Package getPackage()

    public Class<? super T> getSuperclass()

    public Class<?>[] getInerfaces()

    public Constructor<?>[] getConstructors()

    public T newInstance()

    public Method[] getMethods()

    public Field[] getFields()

    2.1获取类的基本信息

     1 package com.example.reflection;
     2 
     3 public class active1 {
     4 
     5     public static void main(String[] args) throws Exception {
     6         // 通过反射获取类对象
     7         Class<?> cp = Class.forName("com.example.reflection.Person");
     8 
     9         // 1.获取类的名字
    10         System.out.println(cp.getName());
    11 
    12         // 2.获取类的包名
    13         System.out.println(cp.getPackage());
    14 
    15         // 3. 获取类的父类
    16         System.out.println(cp.getSuperclass().getName());
    17 
    18         // 4. 获取类继承的接口
    19         for (Class<?> c1 : cp.getInterfaces()) {
    20             System.out.println(c1.getName());
    21         }
    22     }
    23 }

    2.2获取类的构造方法,并创建对象

     1 package com.example.reflection;
     2 
     3 import java.lang.reflect.Constructor;
     4 
     5 public class active2 {
     6     public static void main(String[] args) throws Exception {
     7         // 通过反射获取类对象
     8         Class<?> cp = Class.forName("com.example.reflection.Person");
     9 
    10         // 1. 获取所有构造函数
    11         for (Constructor<?> ct : cp.getConstructors()) {
    12             System.out.println(ct.toString());
    13         }
    14 
    15         // 2. 获取类的无参构造方法
    16         Constructor<?> ct2 = cp.getConstructor();
    17         Person p = (Person) ct2.newInstance(null);
    18         // p = (Person) cp.newInstance();//简写形式
    19         System.out.println(p.toString());
    20 
    21         // 3.获取类的有参构造方法
    22         Constructor<?> ct3 = cp.getConstructor(String.class, int.class);
    23         Person p2 = (Person) ct3.newInstance("张三", 18);
    24         System.out.println(p2.toString());
    25     }
    26 }

    2.3获取类的方法,并调用

     1 package com.example.reflection;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 public class active3 {
     6 
     7     public static void main(String[] args) throws Exception {
     8         // 通过反射获取类对象
     9         Class<?> cp = Class.forName("com.example.reflection.Person");
    10         Person p = (Person) cp.newInstance();
    11 
    12         System.out.println("获取公开的方法和继承的方法");
    13         for (Method m : cp.getMethods()) {
    14             System.out.println(m.toString());
    15         }
    16 
    17         System.out.println("获取所有方法,不含继承的");
    18         for (Method m : cp.getDeclaredMethods()) {
    19             System.out.println(m.toString());
    20         }
    21 
    22         System.out.println("获取公共方法");
    23         Method eat = cp.getMethod("eat", String.class);
    24         eat.invoke(p, "牛肉面");
    25         
    26         System.out.println("获取私有方法");
    27         Method priv = cp.getDeclaredMethod("prviMethod");
    28         priv.setAccessible(true);//允许执行私有方法
    29         priv.invoke(p, null);
    30         
    31         System.out.println("获取静态方法");
    32         Method stac = cp.getDeclaredMethod("staticMethod");
    33         stac.invoke(p, null);
    34     }
    35 }

    2.4实现一个可以调用任何对象的任何方法的方法

     1 package com.example.reflection;
     2 
     3 import java.lang.reflect.Method;
     4 import java.util.Properties;
     5 
     6 public class active4 {
     7 
     8     public static void main(String[] args) throws Exception {
     9         // TODO Auto-generated method stub
    10         Properties ps = new Properties();
    11         invoeAny(ps, "setProperty", new Class[] { String.class, String.class }, "username", "zhangsan");
    12         System.out.println(ps.getProperty("username"));
    13     }
    14 
    15     public static Object invoeAny(Object obj, String methodName, Class<?>[] types, Object... args) throws Exception {
    16         Class<?> cp = obj.getClass();
    17         Method m = cp.getMethod(methodName, types);
    18         return m.invoke(obj, args);
    19     }
    20 }

    2.5 获取属性

     1 package com.example.reflection;
     2 
     3 import java.lang.reflect.Constructor;
     4 import java.lang.reflect.Field;
     5 import java.util.Properties;
     6 
     7 public class active5 {
     8     public static void main(String[] args) throws Exception {
     9         // 通过反射获取类对象
    10         Class<?> cp = Class.forName("com.example.reflection.Person");
    11         Constructor<?> ct = cp.getConstructor(String.class, int.class);
    12         Person p = (Person) ct.newInstance("张三", 18);
    13 
    14         // 公共和继承的属性
    15         for (Field f : cp.getFields()) {
    16             System.out.println(f.toString());
    17         }
    18 
    19         for (Field f : cp.getDeclaredFields()) {
    20             System.out.println(f.toString());
    21         }
    22 
    23         // 获取属性
    24         Field name = cp.getDeclaredField("name");
    25         System.out.println(name);
    26 
    27         // 给属性赋值
    28         name.set(p, "法外狂徒");
    29         System.out.println(p.getName());
    30     }
    31 }

  • 相关阅读:
    Redis应用----消息传递
    Memcache存储机制与指令汇总
    文本挖掘预处理之向量化与Hash Trick
    证书(Certificate)与描述文件(Provisioning Profiles)
    IOS使用命令行打包
    IOS使用xcode编译代码
    IOS使用SourceTree
    docker修改docker0 mtu
    linux开机自启动
    设计模式
  • 原文地址:https://www.cnblogs.com/asenyang/p/14384495.html
Copyright © 2020-2023  润新知