package Demo.java3.反射;
import org.junit.jupiter.api.Test;
import java.io.Serializable;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.Arrays;
*/
import static java.lang.annotation.ElementType.*;
public class 反射的所有使用方法 {
public static void main(String[] args) {
}
@Test //创建Class的三种方法
void abc_00() throws ClassNotFoundException {
//方式1
Class c1=Username.class ; //找到位置
System.out.println(c1);
//方式2
Username use=new Username();
Class c2=use.getClass(); //找到当前类位置
System.out.println(c2);
//方式3
Class c3 = Class.forName("class Demo.java3.反射.Username");//通过全名找到位置
System.out.println(c3);
//方式四(通过类的加载器)
ClassLoader loa = Username.class.getClassLoader(); //获取此类的类加载器
Class c4=loa.loadClass("class Demo.java3.反射.Username");
System.out.println(c4);
}
@Test //获取所有属性的类型以及修饰符
void abc_0()
{
Class c1=Person1.class;
//返回所有自己声明的public和父类有的私有属性
Field[] fields = c1.getFields();//获取所有的public属性(继承了父类还会找父类的所有属性)
for(Field f:fields)
{
System.out.println(f);
}
System.out.println();
System.out.println("返回该类:");
//反回该类的所有属性包括private属性的
Field[] fields1 = c1.getDeclaredFields();//返回该类的所有属性(包括private属性的) 但不包含父类的属性
for (Field f1:fields1)
{
System.out.println(f1);
}
}
@Test //权限修饰符数据类型
void abc_1()
{
Class clazz=Person1.class;
Field[] fields = clazz.getDeclaredFields(); //返回该类的所有属性(包括private属性的) 但不包含父类的属性
for(Field f:fields)
{
//权限修饰符
int i = f.getModifiers(); //输出0,1,2,3,4这样子的
System.out.print(Modifier.toString(i)+" "); //转换回来成普通类型的 //private public
//数据类型
Class type=f.getType(); //返回:class java.lang.String int int
System.out.println(type); //class java.lang.String
//变量名称
String name = f.getName(); //返回: name,age,id //变量名称
System.out.println(name);//输出他的变量名
}
}
@Test//获取所有方法名
void abc_2()
{
Class c1=Person1.class;
//返回所在类及其父类的方法(public方法):
// Method[] methods = c1.getMethods();//返回所在类及其父类的方法(public方法)
// for (Method m:methods)
// {
// System.out.println(m);
// }
//返回所在类的方法
Method[] methods1 = c1.getDeclaredMethods();//返回所在类的方法(所有权限的方法) 和类名称
for (Method method :methods1)
{
System.out.println(method);
}
}
@Test // @ 注解 权限修饰符 方法名(参数类型1,形参名1) 返回值类型
void abc_3()
{
Class c1=Person1.class;
Method[] methods = c1.getDeclaredMethods();//返回所在类的方法(所有权限的方法)
//获取注解
for(Method m1:methods)
{
//获取方法声明的注解
Annotation[] annotations = m1.getAnnotations(); //可能会有多个注解所以用数组
for (Annotation a1:annotations)
{
System.out.println("获取到的注解:"+a1);//获取注解的值
}
//获取所有的方法的(public private)权限修饰符加+返回值类型+方法名+形参
// private void breath() private 权限修饰符 void 返回值类型 breath 方法名
System.out.println("权限修饰符"+Modifier.toString (m1.getModifiers())+" "
+"返回值类型:"+m1.getReturnType().getName()+" "
+"方法名:"+m1.getName()//方法名
);//获取所有的方法的(public private)等权限修饰符 加返回值类型 方法名
//形参列表
System.out.print("(");
Class[] types = m1.getParameterTypes(); //获取里面的所有返回值
if(types!=null&&types.length!=0) //不为空或者length不为0
{
for (Class c:types)
{
System.out.print("形参:"+c.getName()); //获取他的形参 (形参:java.lang.Object)
}
}
System.out.print(")");
//抛出的异常
Class[] ex= m1.getExceptionTypes(); //获取抛出的异常
if(ex!=null&&ex.length!=0) //如果为0或者为空就是啥也没有
{
for(int i=0;i implements Serializable //父类
{
private char gender;
public double weight;
private void breath()
{
System.out.println("生物呼吸");
}
private void eat()
{
System.out.println("生物吃东西 ");
}
}
@MyAnnotation(value = "我是运行时类的注解")
class Person1 extends Creature implements Comparable,MyInterface //主类
{
//属性区
private String name;
int age;
public int id;
//构造函数区
public Person1() { //空参构造器
}
@MyAnnotation(value = "1234")
private Person1(String name) { //一个参数构造器
this.name=name;
}
private Person1(String name,int age) { //两个参数构造器
this.name=name;
this.age=age;
}
//方法区
private String show(String nation) //1
{
System.out.println("我的国籍是:"+nation);
return nation;
}
public String display(String interests) throws Exception //2
{
return interests;
}
public void info() { //3
System.out.println("我是一个人");
}
public int compareTo(String o) { //4
return 0;
}
private static String showDesc(String name)
{
System.out.println("调用了我");
return name;
}
@Override
public String toString() {
return "Person1{" +
"name='" + name + ''' +
", age=" + age +
", id=" + id +
'}';
}
}
interface MyInterface //接口
{
void info();
}
@Target({TYPE,FIELD,METHOD, PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{ //注解
String value() default "hello";
}