• Java反射机制(Class类的使用)


    1:通过无参构造实例化对象
    package cn.itcast;
    /*
     * 通过无参构造实例化对象
     * 通过Class类本身实例化对象,使用newInstance方法
     * 需要注意的是:实例化类中存在一个无参构造的方法,如果不存在将无法实例化Person对象;
     * 如果没有则报异常:InstantionException
     * */
    public class ClassDemo03 {
         public static void main(String[] args) {
              
               //创建Class对象
               Class class1 = null;
               try {
                  class1=Class. forName("cn.itcast.Person");
              } catch (ClassNotFoundException e) {
                   // TODO Auto-generated catch block
                  e.printStackTrace();
              }
               //声明Person对象
              Person p1 = null;
               //实例化Person对象
               try {
                   p1 = (Person) class1.newInstance();
              } catch (InstantiationException e) {
                   // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IllegalAccessException e) {
                   // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              System. out.println(p1);
              
               p1.setName("小李");
               p1.setAge(20);
               p1.setSex('男');
              System. out.println(p1);
         }    
    }
    打印结果:
    Person [name=null, age=0,sex= ]
    Person [name=小李, age=20, sex=男]
    

      

    2:通过有参构造实例化对象
    如果实例化类中确实没有无参构造,就需要明确地调用类中的构造方法,将参数传递进去之后就可以才可以实例化操作;
    public final class Constructor<T>
    extends AccessibleObject
    implements GenericDeclaration, Member
    
    操作步骤:
    1:获取要实例化类的带参构造
    Constructor<?> c2 = class1.getConstructor(String. class,int.class, char.class);
    //获取全部的构造方法   
    Constructor[] c1 = class1.getConstructors();
    for (Constructor getCon : c1) {
        //输出的全是public方法
        System. out.println(getCon);
     }
    打印:
    public cn.itcast.Person()
    public cn.itcast.Person(java.lang.String,int,char)
    
    2:通过c1=newInstance(),实例化对象
    
    代码展示:
    
    package cn.itcast;
    
    import java.lang.reflect.Constructor;
    
    public class ClassDemo04 {
         public static void main(String[] args) {
              
               //创建Class对象
              Class<?> class1 = null;
               try {
                  class1=Class. forName("cn.itcast.Person");
              } catch (ClassNotFoundException e) {
                   // TODO Auto-generated catch block
                  e.printStackTrace();
              }
               //声明Person对象
              Person p1 = null;
              
               //获取全部的构造方法    
              
               Constructor[] c1 = class1.getConstructors();
               for (Constructor getCon : c1) {
                   //输出的全是public方法
                  System. out.println(getCon);
    //            System.out.println(getCon.getName());
    //             System.out.println(getCon.getParameterTypes());
    //            System.out.println(getCon.getModifiers());
              }
              
               try{
                   //获取Person类中的带参构造
                  Constructor<?> c2 = class1.getConstructor(String.class ,int.class, char.class );
                   p1 = (Person) c2. newInstance("小张",24, '男');
              } catch(Exception e){
                  e.printStackTrace();
              }
              System. out.println(p1);
         }
    }
    

      

    package cn.itcast;
    
    public class Person {
    	private String name;
    	private int age;
    	private char sex;
    
    	public Person() {
    		super();
    	}
    
    	public Person(String name, int age, char sex) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.sex = sex;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public char getSex() {
    		return sex;
    	}
    
    	public void setSex(char sex) {
    		this.sex = sex;
    	}
    	public void eat()
    	{
    		System.out.println("吃了");
    	}
    
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    	}
    
    }
    

      

  • 相关阅读:
    Regional Changchun Online--Elven Postman(裸排序二叉树)
    动态规划01背包记录
    hdoj 2546 饭卡
    hdoj 2553 N皇后问题【回溯+打表】
    nyoj 282 You are my brother
    hdoj 1231 最大连续子序列
    hdoj 1003 Max Sum
    2015年6月24
    poj 1338 Ugly Numbers
    poj 3979 分数加减法
  • 原文地址:https://www.cnblogs.com/qlwang/p/5645678.html
Copyright © 2020-2023  润新知