//注解
@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();
}
}
});
}
}