首页 > 代码库 > Java学习笔记--反射
Java学习笔记--反射
Java学习笔记--反射
待完成
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); } }
Java学习笔记--反射
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。