反射,注解

//注解
@UserInfo(userName = "枫叶",userPassword = "123456")
public class Person {
    public String nikeName="枫叶";
    public String name;

    private String age;
    private String idNum;
    public Person() {

    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String nikeName, String name) {
        this.nikeName = nikeName;
        this.name = name;
    }

    private Person(String nikeName, String name,String age){
        this.nikeName = nikeName;
        this.name = name;
        this.age=age;
    }
    public void eat(){
        Log.d("nikeName",nikeName + "吃饭");
    }
    public void sleep(){
        Log.d("nikeName",nikeName + "睡觉");
    }
    public void hit(int count){
        Log.d("nikeName",nikeName + "走路"+count+"次");
    }
}

//注解接口

@Retention(RetentionPolicy.RUNTIME)
public @interface UserInfo {
    String userName();
    String userPassword();
}
public class Test {

    private Context context;

    public Test(Context context) {
        this.context = context;
    }

    public void testReflect() throws Exception {
        //获取class对象
        Class<Person> personClass = Person.class;
        Class aClass = new Person().getClass();
        //要使用类的全路径名
        Class person = Class.forName("com.bw.fengye.marquee.Person");

        //获取所用公共(public)属性
        Field[] fields = personClass.getFields();
        //获取单个属性
        Field nikeName = personClass.getField("nikeName");

        //获取所用属性(所有声明的属性)
        Field[] declaredFields = personClass.getDeclaredFields();
        Field idNum = personClass.getDeclaredField("idNum");

        //获取无参构造方法东方不败
        Constructor[] constructors = personClass.getConstructors();
        Constructor[] declaredConstructors = personClass.getDeclaredConstructors();

        //获取有参构造方法
        Constructor declaredConstructor = personClass.getDeclaredConstructor(String.class);
        Constructor declaredConstructor1 = personClass.getDeclaredConstructor(String.class, String.class);
        Constructor<Person> declaredConstructor2 = personClass.getDeclaredConstructor(String.class, String.class, String.class);

        //通过实例化对象
        Person person1 = personClass.newInstance();
        Person o = (Person) declaredConstructor1.newInstance("林青霞", "东方不败");

        //获取所有公共方法
        Method[] methods = personClass.getMethods();
        //获取所有的方法
        Method[] declaredMethods = personClass.getDeclaredMethods();

        //获取单个public方法
        Method eat = personClass.getMethod("eat");
        Method hit = personClass.getMethod("hit", int.class);

        //获取单个私有方法
        Method sleep = personClass.getDeclaredMethod("sleep");

        //调用方法
        eat.invoke(person1);
        eat.invoke(o);
        hit.invoke(person1,10);
        hit.invoke(o,100);

        //更改public属性值
        nikeName.set(o,"王祖贤");
        idNum.setAccessible(true);
        idNum.set(o,"18");


        Class<Person> personClass1 = Person.class;
        UserInfo annotation = personClass1.getAnnotation(UserInfo.class);
        String name = annotation.userName();
        String password = annotation.userPassword();
        if(name.equals("枫叶")){
            Toast.makeText(context,"名号没错",Toast.LENGTH_SHORT).show();
            if(password.equals("123456")){
                Toast.makeText(context,"密码没错",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(context,"有错",Toast.LENGTH_SHORT).show();
        }

    }
}

//单机之后调用Test()方法

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person=new Person();
                person.eat();
                person.sleep();
                person.hit(5);
                Test test=new Test(MainActivity.this);
                try {
                    test.testReflect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值