/**
* Created by muhanxi on 17/11/28.
*/
public class User {
public String name;
private String age;
public User(){}
public User(String name,String age){
this.name = name;
this.age = age;
}
@Bwei(name = "15099D" ,age = 23)
public void sleep(){
System.out.println("User sleep = ");
}
public void eat(String eat,int time){
System.out.println("User eat = "+ eat + " " + time);
}
public void eat(String eat){
System.out.println("User eat = "+ eat);
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
============以上是user类
package com.example.muhanxi.frescodemo;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by muhanxi on 17/11/28.
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
//@Inherited
public @interface Bwei {
public String name() default "1509D";
public int age() default 22;
}
=======================
package com.example.muhanxi.frescodemo;
/**
* Created by muhanxi on 17/11/28.
*/
public class Person {
public String id;
public Person() {
this("1");
}
public Person(String id){
this.id = id;
}
public void printPerson(){
System.out.println("Person id = " + id);
}
}
========================
package com.example.muhanxi.frescodemo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by muhanxi on 17/11/28.
*/
public class Test {
public static void main(String [] args){
// User user = new User();
try {
Class clazz = Class.forName("com.example.muhanxi.frescodemo.User");
//获取当前类的所有构造方法
// Constructor [] constructors = clazz.getDeclaredConstructors();
//获取当前class 的所有构造方法 public
// Constructor [] constructors = clazz.getConstructors();
// for(Constructor constructor : constructors){
// System.out.println("constructor = " + constructor);
// }
// Constructor constructor = constructors[1];
// // 设置允许访问
// constructor.setAccessible(true);
// User user = (User) constructor.newInstance("1509D",11);
// System.out.println("user = " + user.toString());
/**
// 通过参数的class 获取制定的构造方法
Constructor constructor = clazz.getConstructor(String.class,int.class);
//创建一个对象
User user = (User) constructor.newInstance("1509D",22);
System.out.println( constructor);
System.out.println("user = " + user.toString());
**/
// Field[] fields = clazz.getFields();
/**
User user = (User) clazz.newInstance() ;
//获取当前类的所有属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields){
//设置可以访问private的属性
field.setAccessible(true);
System.out.println("field = " + field);
// 设置user 对象属性值为11
field.set(user,"11");
//获取user对象 的属性值
Object o = field.get(user);
System.out.println("o = " + o);
}
System.out.println("user = " + user.toString());
**/
// Method [] methods = clazz.getDeclaredMethods();
// User user = (User) clazz.newInstance();
// for(Method method : methods){
// method.setAccessible(true);
// System.out.println("method = " + method);
// }
//
//
//
// Object toString = methods[0].invoke(user,null);
// 调用的是eat 参数是1509D
// Object eat = methods[1].invoke(user,"1509D");
// System.out.println("toString = " + eat);
// name 方法名
// String.class 参数的class
// Method method = clazz.getMethod("eat",String.class);
// 执行 user对象身上的方法 参数是1509
// method.invoke(user,"1509");
Method method = clazz.getMethod("sleep",null);
//判断方法上是否有这个注解
boolean isEmpty = method.isAnnotationPresent(Bwei.class);
if(isEmpty){
// 获取对应的注解
Bwei bwei = method.getAnnotation(Bwei.class);
System.out.println("bwei = " + bwei.age() + " "+ bwei.name());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}