java反射的使用简单例子

1.反射在框架中使用的比较多,像工厂模式,装饰者模式

我理解的是:反射就是在主要就是用在运行状态下、能够像开发模式一样能够正常获取对象进行编程的一种手段

1.1普通的反射能获得public的方法和属性

Constructor[] getConstructors():得到所有的public构造方法
Constructor getConstructor(Class… parameterTypes):得到单个的public构造方法:

Method[] getMethods()
Method getMethod() 获取自身能用所有的公共方法。1.类本身的public 2.继承父类的public 3.实现接口的public

Field getField(“name”)

1.2暴力反射能获得public、protected、默认、private的方法和属性

public Constructor[] getDeclaredConstructors():获取所有的构造方法(包括私有、受保护、默认、公有)
public Constructor getDeclaredConstructor(Class… parameterTypes):获取"某个构造方法"可以是私有的,或受保护、默认、公有;

Method[] getDeclaredMethods()
Method getDeclaredMethod() 获取本类中的所有方法,包括私有的(private、protected、默认以及public)的方法。

Field getDeclaredField(“name”)

getDeclaredMethods()方法在java.lang包中可用。
getDeclaredMethods()方法用于返回表示所有方法(即,它是私有方法,公共方法,受保护方法还是默认方法)的Method对象数组,但不包括继承的方法。
getDeclaredMethods()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回方法对象数组时, getDeclaredMethods()方法可能会引发异常。
SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。
2.准备
package com.josion.export.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@TableName("WARES")
public class Wares {
	@TableId(value="ID_",type = IdType.AUTO)
	private Integer id;
	
	@TableField("NAME_")
	private String name;
	
	@TableField("STOCK_")
	private Integer stock;

}
3.反射有三种方式获取Class对象,结果都是相同的
public void reflex() {
       //第一种方法,使用所自定义类中的默认父类Object的class方法
       Wares wares = new Wares();  //实例化一个对象
       Class<? extends Wares> waresClass = wares.getClass();
       
       //第二种方法 访问静态属性class获取
       Class<Wares> waresClass1 = Wares.class;
       
       //第三种方法
       try {
           Class<?> waresClass2= Class.forName("com.josion.export.pojo");
           System.out.println(waresClass+"\n"+waresClass1+"\n"+waresClass2);

//打印结果
//class com.josion.export.pojo.Wares
//class com.josion.export.pojo.Wares
//class com.josion.export.pojo.Wares

       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
	
}
4.通过字段获取对象
 //第三种方法
        try {
            Class<?> waresClass2 = Class.forName("com.josion.export.pojo.Wares");
            
            Field name = waresClass2.getDeclaredField("name");
            name.setAccessible(true);
            
            //获取无参构造
            Constructor<?> c = waresClass2.getDeclaredConstructor();
            
        	c.setAccessible(true);
        	Wares w = (Wares) c.newInstance();
        	
        	name.set(w, "张三");
        	System.out.println(w);
        	System.out.println(name);

//打印结果
//Wares(id=null, name=张三, stock=null)
//private java.lang.String com.josion.export.pojo.Wares.name

        } catch (Exception  e) {
            e.printStackTrace();
        }
4.通过方法获取对象
在这里插入代码片
//第三种方法
        try {
            Class<?> waresClass2 = Class.forName("com.josion.export.pojo.Wares");
					//获取类中所有的方法,如果不知道类中有哪些方法,可以使用这个方法看一下
            //Method[] mm = waresClass2.getDeclaredMethods();
            
           
            
            Constructor<?> c = waresClass2.getDeclaredConstructor(Integer.class,String.class,Integer.class);
            
            Wares w = (Wares) c.newInstance(10086,"李四",110);
            
            
            Method m = waresClass2.getMethod("setStock",Integer.class);
            
            m.invoke(w,122); //method .invoke(w对象,"参数");
            
            System.out.println(w);
            System.out.println(c);
//打印结果
//Wares(id=10086, name=李四, stock=122)
//public com.josion.export.pojo.Wares(java.lang.Integer,java.lang.String,java.lang.Integer)

		} catch (Exception  e) {
            e.printStackTrace();
            
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值