Java_反射技术_简述

学习笔记:反射


提出一个问题

String classPath=”cn.itcast.Cat”

需求,请把该类的所有方法和属性得到.



反射原理图:


1.  使用反射技术得到构造函数,并实例化对象

所有的操作都是对Cat.java

packagecn.itcast.reflection;
 
importjava.util.List;
 
public class Cat {
 
    public Stringname=”麦当劳”;
    private int age;
    private String[]foods;
    public Cat(){
      
       // TODO Auto-generated constructor stub
    }
    publicCat(String []foods){
       this.foods=foods;
    }
   
    public void show(){
       System.out.println("猫猫叫w");
    }
    //添加一个带有参数的函数
    public void show(String name){
       System.out.println("猫猫叫"+name);
    }
    //有两个参数的普通函数
    public void show(String name,int age){
       System.out.println(name+" 年龄是"+age);
    }
   
    //猫猫数数
    private void count(List list){
      
       for(Object obj: list)
       {
           System.out.println(obj);
       }
    }
   
    publicCat(String name,int age){
       this.name=name;
       this.age=age;
    }
 
    public StringgetName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    public int getAge() {
       return age;
    }
 
    public void setAge(int age) {
       this.age = age;
    }
 
    publicString[] getFoods() {
       return foods;
    }
 
    public void setFoods(String[] foods) {
       this.foods = foods;
    }
   
}


 

案例1

//使用默认的构造函数来实例化对象
    private static void test1() throws InstantiationException,
           IllegalAccessException {
       // TODO Auto-generated method stub
       Class clazz=Cat.class;
       //实例化Cat
       Cat cat=(Cat) clazz.newInstance();
       //测试
       cat.show();
    }


案例2

//用 public Cat(String name,int age)来实例化对象
    private static void test2() throws NoSuchMethodException,
           SecurityException,InstantiationException, IllegalAccessException,
           IllegalArgumentException,InvocationTargetException {
      
       Class clazz =Cat.class;
       //得到该构造方法,希望得到
       Constructorc=clazz.getConstructor(String.class,int.class);
      
       //创建实例
       Cat cat=(Cat) c.newInstance("小白",2); //相当于 Cat cat=new Cat("小白",2);
       System.out.println(cat.getName());
       cat.show();
    }


案例3

//得到 public Cat(String []foods) 构造函数
    private static void test3() throws NoSuchMethodException,
           SecurityException,InstantiationException, IllegalAccessException,
           IllegalArgumentException,InvocationTargetException {
      
       //1.得到Class 对象
       Class clazz = Cat.class;
       //2.通过clazz得打构造
       Constructor c=clazz.getConstructor(String[].class);
       //3.通过c 来创建Cat 实例
       //Cat cat=(Cat) c.newInstance((Object) new String[]{"鱼","老鼠"});//因为newInstance接受可变参数,"鱼","老鼠" // newInstance("鱼","老鼠")
       Cat cat=(Cat) c.newInstance(new Object[]{ new String[]{"鱼","老鼠"}});//因为newInstance接受可变参数,"鱼","老鼠" // newInstance("鱼","老鼠")
       cat.show();
    }


2. 使用反射技术,得到成员函数,并调用.

案例1

//反射 public void show(),并调用.
    private static void test1() throws Exception {
      
       //1.得到Class对象
       Class clazz = Class.forName("cn.itcast.reflection.Cat");
       //2.得到show方法
       Method m=clazz.getMethod("show",null);
       //3.调用
       m.invoke(clazz.newInstance(),null); //同学会不好理解clazz.newInstance().invoke(m,"");
    }


案例2

反射public void show(String name)
    private static void test2() throws Exception {
       //反射public void show(String name)
       Class clazz = Class.forName("cn.itcast.reflection.Cat");
       //得到show(String name)
       Method m=clazz.getMethod("show",String.class);
       //调用
       m.invoke(clazz.newInstance(), "宋江"); // show("宋江");
    }

案例3

//反射 public void show(String name,intage)
    private static void test3() throws Exception {
      
       Class clazz = Class.forName("cn.itcast.reflection.Cat");
       Method m=clazz.getMethod("show", String.class,int.class);
       m.invoke(clazz.newInstance(), "贾宝玉",12);
    }


案例4

//反射private void count(List list)
    private static void test4() throws Exception {
      
       Class clazz = Class.forName("cn.itcast.reflection.Cat");
       //Method m=clazz.getMethod("count", List.class);//当一个函数是私有的,则不能使用 getMethod
       Method m=clazz.getDeclaredMethod("count", List.class);
       m.setAccessible(true);//暴力访问.
       List al=new ArrayList();
       al.add(1);al.add(3);
       m.invoke(clazz.newInstance(), al);
    }

3.  使用反射得到字段

案例1:

//反射public  String name="麦当劳";
    private static void test1() throws Exception {
      
       //Cat cat=new Cat();
       Class clazz=Cat.class;
       Field f=clazz.getField("name");
      
       //取得f的值.
       String name=(String)f.get(clazz.newInstance()); //cat.get(f); //返回Object就是name的值
       System.out.println(name);
    }

案例2:

//得到private int age;
	private static void test2() throws Exception {
		
		Class clazz = Cat.class;
		Field f=clazz.getDeclaredField("age");
		f.setAccessible(true);
		//这是创建猫的实例
		Cat cat=(Cat) clazz.newInstance();
		//给f字段赋值.cat表示给哪个实例赋值.
		f.set(cat, 13);
		//取出f.get(cat) 取出cat这个实例的f字符的值
		System.out.println(f.get(cat));
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值