//Creature类
public class Creature<T> implements Serializable {
private char gender;
public double weight;
private void breath(){
System.out.println("生物呼吸");
}
public void eat(){
System.out.println("生物吃东西");
}
}
//MyAnnotation注解
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}
//MyInterface接口
public interface MyInterface {
void info();
}
//Person1类
@MyAnnotation(value = "hi")
public class Person1 extends Creature<String> implements Comparable<String>,MyInterface{
private String name;
int age;
public int id;
public Person1(){
}
@MyAnnotation(value = "abc")
private Person1(String name){
this.name = name;
}
Person1(String name,int age){
this.name = name;
this.age = age;
}
@MyAnnotation
private String show(String nation){
System.out.println("我的国籍是: " + nation);
return nation;
}
public String display(String interests){
return interests;
}
@Override
public void info() {
System.out.println("我是一个人");
}
@Override
public int compareTo(String o) {
return 0;
}
}
/*
获取当前运行时类的属性结构
*/
public class FieldTest {
@Test
public void test1(){
Class clazz = Person1.class;
//获取属性结构
//getFields():获取当前运行时类及其父类中声明为public访问权限的属性
Field[] fields = clazz.getFields();
for (Field f : fields){
System.out.println(f);
}
System.out.println("***********************");
//getDeclaredFields():获取当前运行时类中声明的所有属性(不包含父类中声明的属性)
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields){
System.out.println(f);
}
}
//权限修饰符 数据类型 变量名
@Test
public void test2(){
Class clazz = Person1.class;
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields){
//1.权限修饰符
int modifiers = f.getModifiers();
// System.out.println(modifiers);
System.out.print(Modifier.toString(modifiers) + "\t");//toString
//2.数据类型
Class type = f.getType();
System.out.print(type.getName() + "\t");
//3.变量名
String fName = f.getName();
System.out.println(fName);
System.out.println();
}
}
}