java基础-反射

原文链接:https://blog.csdn.net/lycorisradiata_1/article/details/48493255

1、forName 方法

forName是一个静态方法,其作用:通过调用来获取类名对应的Class对象,同时将Class对象加载进来。

如果将类名保存在字符串(如xml)中,就可以在程序运行时,动态调用加载。

注意:只有调用的参数是类名或者方法时,才可用。

2、newInstance()方法

作用:将对象实例化。返回类型为Object。与new的区别在于,new可以带参,而newInstance()不可以,一边初始化无参类。通常与forName()配合使用。

3.getMethod()方法

getMethod方法与getField方法类似,getField方法根据表示域名的字符串,返回一个Field对象。而getMethod方法则根据方法名称和相关参数,来定位需要查找的Method对象并返回。

getMethod与getDeclareMethods方法的区别在于,后者返回一个Method对象数组,需要自己在结果中查找所需Method对象。

原型: Method getMethod(String name,Class...parameterTypes)

参数解释:name: method的名称

parameterTypes:method的参数类型的列表(参数顺序需按声明method时的参数列表排列)

返回:符合method名称和参数的method对象

4、invoke方法

作用:调用包装在当前Method对象中的方法。

原型:Object invoke(Object obj,Object...args)

参数解释:obj:实例化后的对象

args:用于方法调用的参数

返回:根据obj和args调用的方法的返回值

Class l = Class.forName("test1.A");
Object obj1 = l.newInstance();
Object[] obj2 = new Object[1];
obj2[0] = new String("hello world");
Method m = l.getMethod("a1",new Class[] { String.class });
Object obj3 = m.invoke(obj1, obj2);



参考:https://www.yiibai.com/javareflect/javareflect_method_getannotation.html

java.lang.reflect.Method.getAnnotation(Class <T> annotationClass)

参数

  • annotationClass - Class对象对相应的注释类型。

返回值

  • 如果存在于此元素,则返回该元素注释指定的注释类型,否则返回为null
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

public class MethodDemo {
   public static void main(String[] args) {
      Method[] methods = SampleClass.class.getMethods();

      Annotation annotation = methods[0].getAnnotation(CustomAnnotation.class);
      if(annotation instanceof CustomAnnotation){
         CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
         System.out.println("name: " + customAnnotation.name());
         System.out.println("value: " + customAnnotation.value());
      }
   }
}

@CustomAnnotation(name="SampleClass",  value = "Sample Class Annotation")
class SampleClass {
   private String sampleField;

   @CustomAnnotation(name="getSampleMethod",  value = "Sample Method Annotation")
   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   } 
}

@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
   public String name();
   public String value();
}

结果
name: getSampleMethod
value: Sample Met
 
  • ? 表示不确定的java类型。
  • T 表示java类型。
  • K V 分别代表java键值中的Key Value。
  • E 代表Element。
参考文档:https://www.jianshu.com/p/fecafcc83fba
 
泛型类:
//此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型
//在实例化泛型类时,必须指定T的具体类型
public class Generic<T>{ 
    //key这个成员变量的类型为T,T的类型由外部指定  
    private T key;

    public Generic(T key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定
        this.key = key;
    }

    public T getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
        return key;
    }
}


//泛型接口
public interface Generator<T> {
    public T test();
}

//泛型方法
public <T> T genericMethod(Class<T> tClass){
        T instance = tClass.newInstance();
        return instance;
}

 

 * 说明:
 *     1)public 与 返回值中间<T>非常重要,可以理解为声明此方法为泛型方法。
 *     2)只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
 *     3)<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。
 *     4)与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。
 
posted @ 2021-08-05 15:20  c++c鸟  阅读(71)  评论(0编辑  收藏  举报