测试需要的类,接口,注解
public interface MyInterface {
void info() throws RuntimeException, IOException;
}
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}
public class Creature implements Serializable {
//性别
private char gender;
//体重
public double weight;
private void breath() {
System.out.println("会呼吸");
}
public void eat() {
System.out.println("会吃饭");
}
}
@MyAnnotation("学生类")
public class Student extends Creature implements Comparable, MyInterface {
public int id;
private String name;
int age;
public Student() {
}
@MyAnnotation("构造器")
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@MyAnnotation("show方法")
private String show(String nation, int age) {
System.out.println("我的国籍是:" + nation +", 我的年龄:" + age);
return nation;
}
public String sports(String yd) {
System.out.println(this.name + " 喜欢的运动是: " + yd);
return yd;
}
@Override
public void info() throws RuntimeException, IOException {
System.out.println("我是一个学生");
}
@Override
public int compareTo(String o) {
return 0;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
'}';
}
public static void staticMethod() {
System.out.println("我是静态方法");
}
}
调用运行时类中指定的结构:属性,方法,构造器
@Test
public void test6() throws Exception {
Class clazz = Student.class;
//获取对象
Student student = clazz.newInstance();
System.out.println("************调用属性************");
//获取运行时类的指定属性
//方式1:getField(): 要求运行时类的属性必须声明成public属性,否侧会出现java.lang.NoSuchFieldException异常
//所以不太采用此方法
// Field age = clazz.getField("age");
//方式2:getDeclaredField():获取指定名的属性,任何权限都可以获取到
Field age = clazz.getDeclaredField("age");
//此方法必须加上,打开权限,保证当前属性可访问,否则访问非public权限属性时,会出现java.lang.IllegalAccessException异常
age.setAccessible(true);
//给指定对象的指定属性赋值
age.set(student, 23);
System.out.println("当前对象:" + student);
//获取指定对象的指定属性值
int student_age = (int) age.get(student);
System.out.println("age = "+student_age);
System.out.println("************调用方法(非静态)************");
//getDeclaredMethod():获取指定的方法,参数1:方法名称;参数2,3...:参数列表
Method show = clazz.getDeclaredMethod("show", String.class, int.class);
//此方法必须加上,打开权限,保证当前所有方法都可访问
show.setAccessible(true);
//invoke(): 参数1:方法的调用者,参数2,3..: 参数值
String nation = (String) show.invoke(student, "中国", 23);
System.out.println("方法返回值:" + nation);
System.out.println("************调用方法(静态)************");
Method staticMethod = clazz.getDeclaredMethod("staticMethod");
staticMethod.setAccessible(true);
//使用类调用静态方法,当staticMethod()方法没有返回值时,invoke()方法则返回null。
//方式一:
staticMethod.invoke(Student.class);
//方式二
staticMethod.invoke(null);
System.out.println("************调用构造器************");
//getDeclaredConstructor():参数即构造器的参数列表
Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class);
//保证构造器可访问
constructor.setAccessible(true);
Student student1 = constructor.newInstance("张三", 23);
System.out.println(student1);
}
运行结果:
************调用属性************
当前对象:Student{id=0, name='null', age=23}
age = 23
************调用方法(非静态)************
我的国籍是:中国, 我的年龄:23
方法返回值:中国
************调用方法(静态)************
我是静态方法
我是静态方法
************调用构造器************
Student{id=0, name='张三', age=23}