java 反射 (转)

大佬1:https://www.sczyh30.com/posts/Java/java-reflection-1/#%E4%BA%8C%E3%80%81%E5%8F%8D%E5%B0%84%E7%9A%84%E4%B8%BB%E8%A6%81%E7%94%A8%E9%80%94

大佬2:https://www.cnblogs.com/Seachal/p/5371733.html

Java反射框架主要提供以下功能:

  • 1.在运行时判断任意一个对象所属的类;
  • 2.在运行时构造任意一个类的对象;
  • 3.在运行时判断任意一个类所具有的成员变量和方法(通过反射甚至可以调用private方法);
  • 4.在运行时调用任意一个对象的方法

反射可以用于判断任意对象所属的类,获得Class对象,构造任意一个对象以及调用一个对象。这里我们介绍一下基本反射功能的实现(反射相关的类一般都在java.lang.relfect包里)。

1、获得Class对象

方法有三种
(1)使用Class类的forName静态方法:

 

1

2

3

4

5

 

public static Class<?> forName(String className)

```

在JDBC开发中常用此方法加载数据库驱动:

```java

Class.forName(driver);

 

(2)直接获取某一个对象的class,比如:

 

1

2

 

Class<?> klass = int.class;

Class<?> classInt = Integer.TYPE;

 

(3)调用某个对象的getClass()方法,比如:

 

1

2

 

StringBuilder str = new StringBuilder("123");

Class<?> klass = str.getClass();

 

2、判断是否为某个类的实例

一般地,我们用instanceof关键字来判断是否为某个类的实例。同时我们也可以借助反射中Class对象的isInstance()方法来判断是否为某个类的实例,它是一个Native方法:

 

1

 

public native boolean isInstance(Object obj);

 

3、创建实例

通过反射来生成对象主要有两种方式。
(1)使用Class对象的newInstance()方法来创建Class对象对应类的实例。

 

1

2

 

Class<?> c = String.class;

Object str = c.newInstance();

 

(2)先通过Class对象获取指定的Constructor对象,再调用Constructor对象的newInstance()方法来创建实例。这种方法可以用指定的构造器构造类的实例。

 

1

2

3

4

5

6

7

 

//获取String所对应的Class对象

Class<?> c = String.class;

//获取String类带一个String参数的构造器

Constructor constructor = c.getConstructor(String.class);

//根据构造器创建实例

Object obj = constructor.newInstance("23333");

System.out.println(obj);

 

4、获取方法

获取某个Class对象的方法集合,主要有以下几个方法:
getDeclaredMethods()方法返回类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。

 

1

 

public Method[] getDeclaredMethods() throws SecurityException

 

getMethods()方法返回某个类的所有公用(public)方法,包括其继承类的公用方法。

 

1

 

public Method[] getMethods() throws SecurityException

 

getMethod方法返回一个特定的方法,其中第一个参数为方法名称,后面的参数为方法的参数对应Class的对象

 

1

 

public Method getMethod(String name, Class<?>... parameterTypes)

 

只是这样描述的话可能难以理解,我们用例子来理解这三个方法:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

 

package org.ScZyhSoft.common;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class test1 {

public static void test() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

Class<?> c = methodClass.class;

Object object = c.newInstance();

Method[] methods = c.getMethods();

Method[] declaredMethods = c.getDeclaredMethods();

//获取methodClass类的add方法

Method method = c.getMethod("add", int.class, int.class);  // int.class就是表示int类型的Class对象,每个类型都有一个表示自己类型的Class对象

//getMethods()方法获取的所有方法

System.out.println("getMethods获取的方法:");

for(Method m:methods)

System.out.println(m);

//getDeclaredMethods()方法获取的所有方法

System.out.println("getDeclaredMethods获取的方法:");

for(Method m:declaredMethods)

System.out.println(m);

}

}

class methodClass {

public final int fuck = 3;

public int add(int a,int b) {

return a+b;

}

public int sub(int a,int b) {

return a+b;

}

}

 

程序运行的结果如下:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

 

getMethods获取的方法:

public int org.ScZyhSoft.common.methodClass.add(int,int)

public int org.ScZyhSoft.common.methodClass.sub(int,int)

public final void java.lang.Object.wait() throws java.lang.InterruptedException

public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException

public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException

public boolean java.lang.Object.equals(java.lang.Object)

public java.lang.String java.lang.Object.toString()

public native int java.lang.Object.hashCode()

public final native java.lang.Class java.lang.Object.getClass()

public final native void java.lang.Object.notify()

public final native void java.lang.Object.notifyAll()

getDeclaredMethods获取的方法:

public int org.ScZyhSoft.common.methodClass.add(int,int)

public int org.ScZyhSoft.common.methodClass.sub(int,int)

 

可以看到,通过getMethods()获取的方法可以获取到父类的方法,比如java.lang.Object下定义的各个方法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值