待完成
http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
http://www.cnblogs.com/jqyp/archive/2012/03/29/2423112.html
http://coolszy.iteye.com/blog/569846
http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html
package com.evor.test; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Test1{ public static void main(String args[]){ Class<?> demo1 = null; //类类型 Class<?> demo2=null; Class<?> demo3=null; try{ demo1=Class.forName("com.evor.test.Person"); //获得类类型 }catch(Exception e){ } demo2 = new Person().getClass(); //另一种获得类类型的方式 demo3 = Person.class; //另一种获得类类型的方式 System.out.println("类名称"+demo1.getName()); System.out.println("类名称"+demo2.getName()); System.out.println("类名称"+demo3.getName()); try { Constructor<?> c1 =demo1.getDeclaredConstructor(); System.out.println(c1); //构造器名 Constructor<?> con = demo2.getDeclaredConstructor(new Class[]{String.class,String.class,String.class,int.class}); System.out.println(con); try { Object obj1 = c1.newInstance(new Object[]{}); //创建一个实例 Object obj2 = con.newInstance(new Object[]{"20000001","李明","男",19}); //创建一个实例 System.out.println(obj1); System.out.println(obj2); demo1.getMethod("getInfo", null); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } } class Person{ private String sno ; private String sname ; private String sex ; private int age; public Person(String sno, String sname , String sex , int age){ this.sno = sno; this.sname = sname; this.sex = sex; this.age = age; } public Person(){ this.sno = "unknow"; this.sname = "unknow"; this.sex = "unknow"; this.age = 0; } public void setSno(String sno){ this.sno = sno; } public void setSname(String sname){ this.sname = sname; } public void setSex(String sex){ this.sex = sex; } public void setAge(int age){ this.age = age; } public String getSno() { return sno; } public String getSname() { return sname; } public String getSex() { return sex; } public int getAge() { return age; } public String getInfo(){ return "学号:"+sno+",姓名:"+sname+",性别:"+sex+",年龄:"+String.valueOf(age); } }
时间: 2024-10-21 17:23:16