java反射调用泛型类方法_廖雪峰Java4反射与泛型-1反射-2访问字段Field和3调用方法Method...

2.字段Field

2.1.通过Class实例获取字段field信息:

getField(name): 获取某个public的field,包括父类

getDeclaredField(name): 获取当前类的某个field,不包括父类

getFields(): 获取所有public的field,包括父类

getDeclaredFields(): 获取当前类的所有field,不包括父类

Hello.java

package com.reflection;

import java.util.ArrayList;

public interface Hello {

ArrayList[] listname = new ArrayList[5];

public void hello();

}

Student.java

package com.reflection;

public class Student implements Hello{

private String name;

public int age ;

public Student(){

this("unNamed");

}

public Student(String name){

this.name=name;

}

public void hello(){

System.out.println(name+" is Student");

}

}

Main.java

package com.reflection;

import java.lang.reflect.Field;

public class Main {

public static void main(String[] args) throws NoSuchFieldException{

Class cls = Student.class;

System.out.println("获取age:"+cls.getField("age"));

System.out.println();

System.out.println("获取父类的listname:"+cls.getField("listname"));

System.out.println();

System.out.println("获取age:"+cls.getDeclaredField("age"));

System.out.println();

try{

System.out.println("获取父类的listname:"+cls.getDeclaredField("listname"));

}catch (Exception e){

System.out.println("获取父类的listname:"+e);

}

System.out.println();

System.out.println("获取全部的field,包含父类:");

for(Field field:cls.getFields()){

System.out.println(field);

}

System.out.println();

System.out.println("获取全部的field,不包括父类:");

for(Field field:cls.getDeclaredFields()){

System.out.println(field);

}

}

}

a94ca46ddd84482bec3cc9be58eb9af8.png

2.2.Field对象包含一个field的所有信息:

getName()

getType()

getModifiers()

JAVA 反射机制中,Field的getModifiers()方法返回int类型值表示该字段的修饰符。

其中,该修饰符是java.lang.reflect.Modifier的静态属性。

对应表如下:

PUBLIC: 1 PRIVATE: 2 PROTECTED: 4 STATIC: 8

FINAL: 16 SYNCHRONIZED: 32 VOLATILE: 64

TRANSIENT: 128 NATIVE: 256 INTERFACE: 512

ABSTRACT: 1024 STRICT: 2048

Main.java

package com.reflection;

import java.lang.reflect.Field;

public class Main {

public static void main(String[] args) throws NoSuchFieldException{

Class cls = Student.class;

Field field1 = cls.getField("age");

System.out.println(field1.getName());

System.out.println(field1.getType());

System.out.println(field1.getModifiers());

}

}

e4e720c2c8227e5ae4058e65ab7f17e8.png

2.3.获取和设置field的值:

get(Object obj):

set(Object,Object)

//疯狂Java840页

import java.lang.reflect.Field;

class Person {

private String name;

private int age;

public String toString(){

return "Person[name:" + name + ", age:" +age +"]";

}

}

public class FieldTest{

public static void main(String[] args) throws Exception{

Class cls = Person.class;

Person p = (Person) cls.newInstance();

Field nameField = cls.getDeclaredField("name");

nameField.setAccessible(true);

nameField.set(p,"小明");

Field ageField = cls.getDeclaredField("age");

ageField.setAccessible(true);

ageField.set(p,12);

System.out.println(p.toString());

System.out.println(ageField.get(p));

System.out.println(nameField.get(p));

}

}

47ec4b432b03226ec0473af0cbc8b808.png

2.4.通过反射访问Field需要通过SecurityManager设置的规则。

通过设置setAccessible(true)来访问非public字段。

3.调用方法Method

3.1通过class实例获取方法Method信息:

getMethod(name,Class...):获取某个public的method(包括父类)

getDeclaredMethod(name,Class...):获取当前类的某个method(不包括父类)

getMethods():获取所有public的method(包括父类)

getDeclaredMethods():获取当前类的所有method(不包括父类)

Animal.java

package com.reflection;

public class Animal {

public void run(){

System.out.println("动物再跑");

}

private void eat(String s,String i){

System.out.println("动物每天能吃"+i+"Kg"+s);

}

}

Dog.java

package com.reflection;

public class Dog extends Animal {

public void bar(){

System.out.println("狗在叫");

}

private void func(){

System.out.println("狗能看家");

}

}

Go.java

package com.reflection;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Go {

public static void main(String[] args) throws ClassNotFoundException,NoSuchMethodException,IllegalAccessException,InstantiationException,InvocationTargetException {

Class cls = Class.forName("com.reflection.Dog");

Dog dog = (Dog)cls.newInstance();

System.out.println("getMethod获取某个public的method,包括父类");

Method pubMethod1 = cls.getMethod("run");

System.out.print(pubMethod1.getName()+":");

pubMethod1.invoke(dog);

Method pubMethod2 = cls.getMethod("bar");

System.out.print(pubMethod2.getName()+":");

pubMethod2.invoke(dog);

System.out.println();

System.out.println("getDeclaredMethod:获取当前类的某个method,不包括父类");

Method subMethod1 = cls.getDeclaredMethod("func");

System.out.println(subMethod1.getName());

Method subMethod2 = cls.getDeclaredMethod("bar");

System.out.println(subMethod2.getName());

System.out.println();

System.out.println("getMethods():获取所有public的method(包括父类)");

for(Method m:cls.getMethods()){

System.out.println(m.getName());

}

System.out.println();

System.out.println("getDeclaredMethods():获取当前类的所有method(不包括父类)");

for(Method m:cls.getDeclaredMethods()){

System.out.println(m.getName());

}

}

}

a51cfa5b387ed6fe5e13c7b50dfddc14.png

## 3.2Method对象包含一个method的所有信息

* getName()

* getReturnType()

* getParameterTypes()

* getModifiers()

```#java

public class Go {

public static void main(String[] args) throws ClassNotFoundException,NoSuchMethodException,IllegalAccessException,InstantiationException,InvocationTargetException {

Class cls = Class.forName("com.reflection.Dog");

Dog dog = (Dog)cls.newInstance();

Method pubMethod1 = cls.getMethod("bar");

System.out.println(pubMethod1.getName());

System.out.println(pubMethod1.getReturnType());

System.out.println(pubMethod1.getParameterTypes());

System.out.println(pubMethod1.getModifiers());

}

}

7b4cfc86f022bfd742c116628ad67b1f.png

## 3.3调用Method:

* Object invoke(Object obj,Object...args)

通过设置setAccessible(true)来访问非public方法

反射调用Method也遵守多态的规则

参见3.1中代码

参考博客:

[https://blog.csdn.net/z69183787/article/details/23852517](https://blog.csdn.net/z69183787/article/details/23852517)

[https://blog.csdn.net/GhostFromHeaven/article/details/6498350](https://blog.csdn.net/GhostFromHeaven/article/details/6498350)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值