getConstructor

来源:互联网 发布:魏娜娣软件测试课件 编辑:程序博客网 时间:2024/04/28 01:35
  1. //新曙光培训@西安
  2. //www.xinshuguang.org
  3. import java.util.*;
  4. class FanSheGouZao{
  5.         int i;
  6.         char c;
  7.         public FanSheGouZao(int k){i=k;System.out.println("i="+i);}
  8.         //如果要用getConstructor,则构造方法前面必须加public
  9.         public FanSheGouZao(char ZiFu){c=ZiFu;System.out.println("c is "+c);}
  10.    
  11. }
  12. public class FanSheGouZaoTest{
  13.       public static void main(String[] args) throws Exception{
  14.            Class type=FanSheGouZao.class;
  15.            type.getConstructor(char.class).newInstance('M');
  16.           
  17.            //这句话相当于
  18.            //new FanSheGouZao(8);
  19.            //new FanSheGouZao('M');
  20.       }
  21. }
  22.         
原创粉丝点击