反射+自定義註解

1.Person類
import android.util.Log;
@UserInfo(username = “de”,userpassword = “12”)
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 age, String idNum) {
    this.nikeName = nikeName;
    this.age = age;
    this.idNum = idNum;
}


public void sleep(){
    Log.d("=======",nikeName+"睡覺");
}
public void eat(int count){
    Log.d("=======",nikeName+"吃飯"+count+"次");
}

}

2.Test
public class Test {
private Context context;

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

public void testReflect() throws Exception{
    //獲取class對象
    Class<Person> personClass = Person.class;
    //獲取屬性
    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<Person> declaredConstructor = personClass.getDeclaredConstructor(String.class);
    Constructor constructor = personClass.getConstructor(String.class, String.class,String.class);
    //通過實例化對象
    Person person = personClass.newInstance();
    Person o = (Person) constructor.newInstance("德倫狗","asfas","dd");
    //獲取所有public方法
    Method[] methods = personClass.getMethods();
    //獲取所有的方法
    Method[] declaredMethods = personClass.getDeclaredMethods();
    //獲取單個public方法
    Method eat = personClass.getMethod("eat",int.class);
    //獲取單個私有方法
    Method sleep = personClass.getDeclaredMethod("sleep");
    //調用方法
    eat.invoke(person,10);
    eat.invoke(o,1100);
    Class<Person> personClass1 = Person.class;
    //獲取某個註解
    UserInfo annotation = personClass1.getAnnotation(UserInfo.class);
    String username = annotation.username();
    String userpassword = annotation.userpassword();
    //校驗
    if (username.equals("德倫")&&userpassword.equals("123")){
        Toast.makeText(context.getApplicationContext(), "沒毛病", Toast.LENGTH_SHORT).show();
    }else {
        Toast.makeText(context.getApplicationContext(), "出錯了", Toast.LENGTH_SHORT).show();
    }
}

}

3.userinfo
@Retention(RetentionPolicy.RUNTIME)
public @interface UserInfo {
String username();
String userpassword();
}
4.MainActivity
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.text2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Test test = new Test(MainActivity.this);
            try {
                test.testReflect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章