Java反射

Java反射为开发者提供了更灵活的方式编程,可以在运行时操纵类的构造方法、属性和方法。

1、调用类的公有方法

Car类作为通过反射获取其isExpensive()方法

package com.reflect;
public class Car {  private String name;    private Long price;       public String getName() {   return name;  }
 public void setName(String name) {   this.name = name;  }
 public Long getPrice() {   return price;  }
 public void setPrice(Long price) {   this.price = price;  }
 public boolean isExpensive(Long price) {         if(price>200000L){          System.out.println("this car price is more than 20w!");             return true;         } else {          System.out.println("this car price is less than 20w!");             return false;         }     } }

写一个方法进行反射调用

public class ReflectTest {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
        Class<?> myTest = Class.forName("com.discover.amazing.reflect.Car");
        Method fun1 = myTest.getMethod("isExpensive", Long.class);
        boolean result =  (Boolean) fun1.invoke(myTest.newInstance(), 500000L);
    }

}


2、操纵类的属性

修改Car类的isExpensive()方法

public class Car {
	private String name;
	
	private Long price;
	
    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getPrice() {
		return price;
	}

	public void setPrice(Long price) {
		this.price = price;
	}

	public boolean isExpensive() {
        if(this.getPrice()>200000L){
        	System.out.println("this car price is more than 20w!");
            return true;
        } else {
        	System.out.println("this car price is less than 20w!");
            return false;
        }
    }
}

通过反射获取类的属性,然后设置该属性的值

public class ReflectTest {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<?> car = Class.forName("com.discover.amazing.reflect.Car");
        Method fun1 = car.getMethod("isExpensive");
        Field priceField = car.getDeclaredField("price");
        Object carInstance = car.newInstance();
        priceField.setAccessible(true);
        priceField.set(carInstance, 150000L);
        boolean result =  (Boolean) fun1.invoke(carInstance);
    }

}


3、操纵类的构造方法

在Car类中增加构造方法

public class Car {
	private String name;
	
	private Long price;
	
    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getPrice() {
		return price;
	}

	public void setPrice(Long price) {
		this.price = price;
	}
	
	public Car(String name, Long price) {
		this.name = name;
		this.price = price;
	}

	public boolean isExpensive() {
        if(this.getPrice()>200000L){
        	System.out.println("this car price is more than 20w!");
            return true;
        } else {
        	System.out.println("this car price is less than 20w!");
            return false;
        }
    }
}


通过反射获取构造方法,通过构造方法实例化一个Car对象,然后调用Car的 isExpensive()方法

public class ReflectTest {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<?> clazz = Class.forName("com.discover.amazing.reflect.Car");
        Constructor<?> carConstructor = clazz.getDeclaredConstructor(String.class, Long.class);
        carConstructor.setAccessible(true);
        Car car = (Car) carConstructor.newInstance("Benz", 500000L);
        car.isExpensive();
    }

}



Nepxion Discovery【探索】使用指南,基于Spring Cloud Greenwich版、Finchley版和Hoxton版而 制作,对于Edgware版,使用者需要自行修改。使用指南主要涉及的功能包括: 基于Header传递的全链路灰度路由,网关为路由触发点。采用配置中心配置路由规则映射在网 关过滤器中植入Header信息而实现,路由规则传递到全链路服务中。路由方式主要包括版本和 区域的匹配路由、版本和区域的权重路由、基于机器IP地址和端口的路由 基于规则订阅的全链路灰度发布。采用配置中心配置灰度规则映射在全链路服务而实现,所有 服务都订阅某个共享配置。发布方式主要包括版本和区域的匹配发布、版本和区域的权重发布 全链路服务隔离。包括注册隔离、消费端隔离和提供端服务隔离,示例仅提供基于Group隔 离。除此之外,不在本文介绍内的,还包括: 注册隔离:黑/白名单的IP地址的注册隔离、最大注册数限制的注册隔离 消费端隔离:黑/白名单的IP地址的消费端隔离 全链路服务限流熔断降级权限,集成阿里巴巴Sentinel,有机整合灰度路由,扩展LimitApp的 机制,通过动态的Http Header方式实现组合式防护机制,包括基于服务名、基于灰度组、基于 灰度版本、基于灰度区域、基于机器地址和端口等防护机制,支持自定义任意的业务参数组合 实现该功能。支持原生的流控规则、降级规则、授权规则、系统规则、热点参数流控规则 全链路灰度调用链。包括Header方式和日志方式,Header方式框架内部集成,日志方式通过 MDC输出(需使用者自行集成) 同城双活多机房切换支持。它包含在“基于Header传递的全链路灰度路由”里 数据库灰度发布。内置简单的数据库灰度发布策略,它不在本文的介绍范围内 灰度路由和发布的自动化测试 license Apache 2.0 maven central v5.4.0 javadoc 5.4.0 build passing Docker容器化和Kubernetes平台的无缝支持部署
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值