zoukankan      html  css  js  c++  java
  • 反射机制 CLass类的使用

    1  通过无参构造实例化对象

     1 package com.matto;
     2 
     3 /**
     4  * Created by matto on 6/15/16.
     5  */
     6 class Person{
     7     private String name ;
     8     private int age;
     9 
    10     public String getName() {
    11         return name;
    12     }
    13 
    14     public void setName(String name) {
    15         this.name = name;
    16     }
    17 
    18     public int getAge() {
    19         return age;
    20     }
    21 
    22     public void setAge(int age) {
    23         this.age = age;
    24     }
    25 
    26     public String toString(){
    27         return "姓名:" + this.name + "年龄:" +this.age ;
    28     }
    29 }
    30 
    31 public class InstanceDemo {
    32     public static void main(String[] args) {
    33         Class<?> c = null;
    34         try {
    35             c = Class.forName("com.matto.InstanceDemo.Person");
    36         } catch (ClassNotFoundException e) {
    37             e.printStackTrace();
    38         }
    39         Person per = null;
    40 
    41         try {
    42             per = (Person) c.newInstance();
    43         } catch (IllegalAccessException e) {
    44             e.printStackTrace();
    45         } catch (InstantiationException e) {
    46             e.printStackTrace();
    47 
    48             per.setName("Matto");
    49             per.setAge(28);
    50             System.out.println(per);
    51         }
    52     }
    53 }

    2  调用有参构造实例化对象

     1 package com.matto;
     2 
     3 import java.lang.reflect.Constructor;
     4 import java.lang.reflect.InvocationTargetException;
     5 
     6 /**
     7  * Created by matto on 6/15/16.
     8  */
     9 class Person{
    10     private String name ;
    11     private int age;
    12 
    13     public Person(String name , int age){
    14         this.setName(name);
    15         this.setAge(age);
    16     }
    17     
    18     public String getName() {
    19         return name;
    20     }
    21 
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25 
    26     public int getAge() {
    27         return age;
    28     }
    29 
    30     public void setAge(int age) {
    31         this.age = age;
    32     }
    33 
    34     public String toString(){
    35         return "姓名:" + this.name + "年龄:" +this.age ;
    36     }
    37 }
    38 
    39 public class InstanceDemo {
    40     public static void main(String[] args) {
    41         Class<?> c = null;
    42         try {
    43             c = Class.forName("com.matto.InstanceDemo.Person");  //传入要实例化的包.类
    44         } catch (ClassNotFoundException e) {
    45             e.printStackTrace();
    46         }
    47         Person per = null;
    48         Constructor<?> cons[] = null;                 //声明一个表示构造方法的数组
    49         cons = c.getConstructors();                  //通过反射取得全部构造函数,此处是以对象数组的方式返回。对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋给数组就行了。
    50 
    51         try {
    52             per = (Person) cons[0].newInstance("Matto",28);    //向构造方法传递参数,因Person只有一个构造方法,所以数组下标为 [0]
    53         } catch (InstantiationException e) {
    54             e.printStackTrace();
    55         } catch (IllegalAccessException e) {
    56             e.printStackTrace();
    57         } catch (InvocationTargetException e) {
    58             e.printStackTrace();
    59         }
    60 
    61         System.out.println(per);                    //Person重写了toString()方法,所以输出结果为  : 姓名: Matto 年龄 :28
    62     }
    63 }
  • 相关阅读:
    EasyUI-datagrid-自动合并单元格(转)
    js中格式化时间字符串
    ext 3.2 tree 在IE10中点击事件失效的bug
    C#中修改Dll文件 (反编译后重新编译)
    GridView内容<br />换行
    使用Aspose.Words把 word转成图片
    判断移动设备访问自动跳转到移动版页面
    jquery mobile界面数据刷新
    Ubuntu16.04下安装Visual Studio Code
    npm 安装vue 报错Failed at the chromedriver@2.46.0 install script 'node install.js'
  • 原文地址:https://www.cnblogs.com/blog4matto/p/5586905.html
Copyright © 2011-2022 走看看