java 练习 反射入门 自定义注解 动态代理

1.设置初始类(银行类)设置私有属性及get set 方法 和 重写toString

package reflect;

public class Account {
    private String ID;
    private String name;
    private String password;
    private double money;
    private double quotaMoney;



    public Account(String ID, String name, String password, double money, double quotaMoney) {
        System.out.println("有参构造器执行");
        this.ID = ID;
        this.name = name;
        this.password = password;
        this.money = money;
        this.quotaMoney = quotaMoney;
    }

    public Account() {
        System.out.println("无参数构造器执行");
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public double getQuotaMoney() {
        return quotaMoney;
    }

    public void setQuotaMoney(double quotaMoney) {
        this.quotaMoney = quotaMoney;
    }

    @Override
    public String toString() {
        return "Account{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", money=" + money +
                ", quotaMoney=" + quotaMoney +
                '}';
    }
}

2.反射

第一步都要获取类

获取私有变量可以暴力获取    cons.setAccessible(true);

package reflect;

import org.junit.Test;

import java.lang.reflect.Constructor;

public class Test1 {
    @Test
    public void getAccount() throws Exception{
        //获取类对象
        Class c = Account.class;
//        //提取全部构造器的对象
//        Constructor[] constructors = c.getConstructors();
//        //遍历构造器
//        for (Constructor constructor : constructors) {
//            System.out.println(constructor.getName()+"===="+constructor.getParameterCount());
//        }

        //提取全部构造器的对象
        Constructor cons = c.getDeclaredConstructor();
        System.out.println(cons.getName()+"===="+cons.getParameterCount());
        //遇到私有构造器暴力反射
        cons.setAccessible(true);//权限打开
        Account a = (Account) cons.newInstance();
        System.out.println(a);

        System.out.println("===================================");

        Constructor cons1 = c.getDeclaredConstructor(String.class, String.class, String.class, double.class, double.class);
        System.out.println(cons1.getName()+"===="+cons1.getParameterCount());
        Account a1 = (Account) cons1.newInstance("1001","xsw","1001",1000.0,1000.0);
        System.out.println(a1);
    }
}

结果

3.获取反射对象  反射赋值

package reflect;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.IDN;
import java.util.ArrayList;

public class Test2 {
    @Test
    public void getDeclaredFields() {
        Class c =Account.class;
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName()+"======"+field.getType());
        }
    }

    @Test
    public void getDeclaredField() throws Exception{
        Class c = Account.class;
        Field field = c.getDeclaredField("money");
//        System.out.println(field.getName()+"=====>"+field.getType());
        //赋值
        field.setAccessible(true);
        Account account = new Account();
        field.set(account,123123);
        System.out.println(account);
        //取值
        double money = (double) field.get(account);
        System.out.println(money);
    }

    @Test
    public void set() throws Exception {
        //存数据  类似翻墙进入女厕所
        ArrayList<Integer> list = new ArrayList<>();
        list.add(23);
        list.add(26);
        System.out.println(list);

        Class c = list.getClass();
        Method add = c.getDeclaredMethod("add",Object.class);
        boolean rs = (boolean) add.invoke(list,"xsw");
        System.out.println(rs);
        System.out.println(list);
    }
}

 

结果

 

4.自定义注解

package reflect;

/**
 * 自定义注解
 */
@Mybokk(name="java",names = {"xsw","dsg"},money = 1990)
public class DemoAnnotation {
    public static void main(String[] args) {
    }
}




package reflect;

public @interface Mybokk {
    String name();
    String[] names();
    double money();//default 9.9 默认值
}

 5.*动态代理

* 1.先走代理  创建接口
* 2.代理可以为方法额外做一些辅助工作
* 3.开放真正触发对象的执行
* 4.回到代理,由代理负责返回结果给方法的调用者
package test3;

public interface Skill {
    void jump();
    void sing();
}
package test3;

public class Star implements Skill {
    private String name;
    public Star(String name){
        this.name = name;
    }

    @Override
    public void jump() {
        System.out.println(name + "开始跳舞");
    }

    @Override
    public void sing() {
        System.out.println(name+"开始唱歌");

    }
}

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class StarAgent {
    /**
     * 设计一个方法返回明星代理对象
     */
    public static Skill getProxy(Star star){
        return (Skill) Proxy.newProxyInstance(star.getClass().getClassLoader(),
                star.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("收款");
                        //去跳舞 method调用对象方法 args代表这个方法的参数
                        Object rs = method.invoke(star,args);
                        System.out.println("收尾款");
                        return rs;
                    }
                });
    }

}
public class proxy {
    public static void main(String[] args) {
        Star star = new Star("杨超越");
//        star.jump();
//        star.sing();
        Skill s2 = StarAgent.getProxy(star);
        s2.jump();
        s2.sing();
    }
}

结果

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值