zoukankan      html  css  js  c++  java
  • 反射

    一:通过反射创建对象

      1.Class clazz = Class.forName("包路径.类名");

      2Constructor c = clazz.newInstance(构造放参数类型.class...);

      3.Person1 p1 = (Person1)c.newInstance("小明",34);

      

     1 package socket;
     2 import java.lang.reflect.Constructor;
     3 public class MySocket {
     4     public static void main(String[] args) throws Exception {
     5         //通过反射得到类的字节码文件,forName里面的参数为包名点类名
     6         Class clazz = Class.forName("socket.Person1");
     7         //通过字节码文件得到类的构造方法
     8         Constructor c = clazz.getConstructor(String.class,int.class);
     9         //通过构造方法创建类对象
    10         Person1 p1 = (Person1) c.newInstance("小明",34);
    11         System.out.println(p1.getName());
    12     }
    13 }
    14 class Person1{
    15     private String name;
    16     private int id;
    17     public Person1(){}
    18     public Person1(String name, int id) {
    19         //super();
    20         this.name = name;
    21         this.id = id;
    22     }
    23     public String getName() {
    24         return name;
    25     }
    26     public void setName(String name) {
    27         this.name = name;
    28     }
    29     public int getId() {
    30         return id;
    31     }
    32     public void setId(int id) {
    33         this.id = id;
    34     }
    35 }
    View Code
    小明

    二:通过反射如何获取类的方法,调用类的方法,代码如下。代码里有详细注释。

     1 package socket;
     2 import java.lang.reflect.Constructor;
     3 import java.lang.reflect.InvocationTargetException;
     4 import java.lang.reflect.Method;
     5 public class MySocket2 {
     6 
     7     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
     8         Class clazz = Class.forName("socket.Person2");
     9         Method[] m = clazz.getMethods();//获取子类和父类 的所有公有的方法
    10         System.out.println(m.length);
    11         for(int i = 0 ; i < m.length; i ++){
    12             System.out.println(m[i]);
    13         }
    14         /**/
    15         //调用方法:1创建对象
    16         Constructor c = clazz.getConstructor(String.class,int.class);
    17         //通过构造方法创建对象
    18         Person2 p = (Person2)c.newInstance("张三",13);
    19         //通过m[i]调用invoke()方法执行
    20         System.out.println(m[0].invoke(p));
    21         //通过getDeclaredMethod方法获取类中所有声明的方法,包括私有方法。
    22         Method m1 = clazz.getDeclaredMethod("print3");
    23         //若为私有方法通过setAccessible()设置该方法可执行
    24         m1.setAccessible(true);
    25         m1.invoke(p);
    26 
    27     }
    28 
    29 }
    30 class Person2{
    31     private String name;
    32     private int id;
    33     public Person2(){}
    34     public Person2(String name, int id) {
    35         //super();
    36         this.name = name;
    37         this.id = id;
    38     }
    39     public void print1(){
    40         System.out.println("共有方法print1执行了");
    41     }
    42     public void print2(){
    43         System.out.println("共有方法print1执行了");
    44     }
    45     private void print3(){
    46         System.out.println("私有方法print1执行了");
    47     }
    48     private void print4(){
    49         System.out.println("私有方法print1执行了");
    50     }
    51     public String getName() {
    52         return name;
    53     }
    54     public void setName(String name) {
    55         this.name = name;
    56     }
    57     public int getId() {
    58         return id;
    59     }
    60     public void setId(int id) {
    61         this.id = id;
    62     }
    63 }
    View Code
     1 15
     2 public java.lang.String socket.Person2.getName()
     3 public int socket.Person2.getId()
     4 public void socket.Person2.setName(java.lang.String)
     5 public void socket.Person2.print2()
     6 public void socket.Person2.print1()
     7 public void socket.Person2.setId(int)
     8 public final void java.lang.Object.wait() throws java.lang.InterruptedException
     9 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    10 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    11 public boolean java.lang.Object.equals(java.lang.Object)
    12 public java.lang.String java.lang.Object.toString()
    13 public native int java.lang.Object.hashCode()
    14 public final native java.lang.Class java.lang.Object.getClass()
    15 public final native void java.lang.Object.notify()
    16 public final native void java.lang.Object.notifyAll()
    17 张三
    18 私有方法print1执行了
    View Code

    三:通过反射获取类的构造方法。

      

     1 package socket;
     2 
     3 import java.lang.reflect.Constructor;
     4 
     5 public class MySocket3 {
     6 
     7     public static void main(String[] args) throws ClassNotFoundException {
     8         Class clazz = Class.forName("socket.Person3");
     9         //以数组的形式获取所有共有构造方法
    10         Constructor[] c = clazz.getConstructors();
    11         for(Constructor x:c){
    12             System.out.println(x);
    13         }
    14         //获取类中所有声明的构造方法,包括私有的构造方法
    15         Constructor[] c2 = clazz.getDeclaredConstructors();
    16         for(Constructor x:c2){
    17             System.out.println(x);
    18         }
    19     }
    20 
    21 }
    22 class Person3{
    23     private String name;
    24     private int id;
    25     private Person3(){}
    26     public Person3(String name, int id) {
    27         //super();
    28         this.name = name;
    29         this.id = id;
    30     }
    31     public void print1(){
    32         System.out.println("共有方法print1执行了");
    33     }
    34     public void print2(){
    35         System.out.println("共有方法print1执行了");
    36     }
    37     private void print3(){
    38         System.out.println("私有方法print1执行了");
    39     }
    40     private void print4(){
    41         System.out.println("私有方法print1执行了");
    42     }
    43     public String getName() {
    44         return name;
    45     }
    46     public void setName(String name) {
    47         this.name = name;
    48     }
    49     public int getId() {
    50         return id;
    51     }
    52     public void setId(int id) {
    53         this.id = id;
    54     }
    55 }
    View Code
    1 public socket.Person3(java.lang.String,int)
    2 private socket.Person3()
    3 public socket.Person3(java.lang.String,int)
    View Code
  • 相关阅读:
    B 基因改造
    A 密码锁
    Leetcode(884)-索引处的解码字符串
    Leetcode(885)- 救生艇
    Leetcode(23)-合并K个排序链表
    关于优先队列的总结II
    重载运算符问题
    Leetcode(22)-括号生成
    Leetcode(102)-二叉树的层次遍历
    Leetcode(82)-删除排序链表中的重复元素 II
  • 原文地址:https://www.cnblogs.com/huxuebing/p/5767630.html
Copyright © 2011-2022 走看看