反射的理解

反射的理解

        反射java语言中的一种机制,通过这种机制可以动态(灵活)的实例化对象
         读写属性,调用方法

Student  对象

package com.Demo;

public class Student {
	private String sid;
	
	protected  boolean  sex;

	private String sname;

	public Integer age;

	public Student() {
		super();
		System.out.println("调用无参构造方法创建了一个学生对象");
	}

	public Student(String sid) {
		super();
		this.sid = sid;
		System.out.println("调用带一个参数的构造方法创建了一个学生对象");
	}

	public Student(String sid, String sname) {
		super();
		this.sid = sid;
		this.sname = sname;
		System.out.println("调用带二个参数的构造方法创建了一个学生对象");
	}

	@SuppressWarnings("unused")
	private Student(Integer age) {
		System.out.println("调用Student类私有的构造方法创建一个学生对象");
		this.age = age;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public void hello() {
		System.out.println("你好!我是" + this.sname);
	}

	public void hello(String name) {
		System.out.println(name + "你好!我是" + this.sname);
	}

	@SuppressWarnings("unused")
	private Integer add(Integer a, Integer b) {
		return new Integer(a.intValue() + b.intValue());
	}
}

反射获取对象的三种方式

         方式一        

               Class  c=Class.forName("路径");

               Object  s=c.newInstance();

        方式二

                 Student  s=Student.class.newInstance();

        方式三

                Student s=new   Student();

                s.getClass().newInstance();

通过反射获取对应的构造方法

       公开的

 Class c= Class.forName("com.Demo.Student");
Constructor con = c.getConstructor(String.class);
  Student s = (Student)con.newInstance("ss");
    System.out.println(s.getSid());

         非公开的

Class c= Class.forName("com.Demo.Student");
Constructor con = c.getDeclaredConstructor(Integer.class);
    	//授权
con.setAccessible(true);
con.newInstance(1);

通过反射获取对应的对象属性

  获取public

   Class<Student>  c= (Class<Student>) Class.forName("com.Demo.Student");
   Student obj = c.newInstance();
    //获取属性  属性名必须一样
     Field f = c.getField("age");
   //给f赋值  第一个参数是实例化对象  
     f.set(obj, 3);
   System.out.println(obj.age);

获取private

  Class<Student>  c= (Class<Student>) Class.forName("com.Demo.Student");
    	   //先实例化对象
    	   Student obj = c.newInstance();
           Field f = c.getDeclaredField("sname");
    	   f.setAccessible(true);
    	   f.set(obj, "ws");
    	   System.out.println(obj.getSname());

获取protected

Class<Student>  c= (Class<Student>) Class.forName("com.Demo.Student");
    	   //先实例化对象
    	   Student obj = c.newInstance();
         Field f = c.getDeclaredField("sex");
    	   f.setAccessible(true);
    	   f.set(obj, true);
    	   System.out.println(obj.sex);

通过反射获取对应对象的方法

        公开的

 Class  c= Class.forName("com.Demo.Student");
    	 
    	 Student s=(Student) c.newInstance();
      //通过反射得到公开方法   方法名  方法的参数
    	 Method m = c.getMethod("hello", null);
       //运行该方法  对象  参数
    	 m.invoke(s, args);

非公开的

 Class  c= Class.forName("com.Demo.Student");
    	 
    	 Student s=(Student) c.newInstance();
      //活得非公开的方法   方法名    参数类型
    	Method m = c.getDeclaredMethod("add",Integer.class,Integer.class);
    	m.setAccessible(true);
    	//有返回直接输出
    	System.out.println(m.invoke(s, 12,32));

通过反射获取对应的修饰符

// 访问修饰符
    	Class  c= Class.forName("com.Demo.Student"); 
    	 Student com = (Student) c.newInstance();
    	 //拿到该属性    protected  4   private 2  public 1  默认的 0   多个修饰符以多个的倍数增长
    	 Field f = c.getDeclaredField("sex");  
    	 //获取该属性的修饰符
    	 int m = f.getModifiers();
    	 System.out.println(m);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值