首页 技术 正文
技术 2022年11月15日
0 收藏 663 点赞 4,458 浏览 1286 个字

一:jdk API中关于两个方法的解释

1:getMethods(),该方法是获取本类以及父类或者父接口中所有的公共方法(public修饰符修饰的)

Class类中getMethods() 与getDeclaredMethods() 方法的区别

2:getDeclaredMethods(),该方法是获取本类中的所有方法,包括私有的(private、protected、默认以及public)的方法。

Class类中getMethods() 与getDeclaredMethods() 方法的区别

二:代码演示

1:定义父类ReflectionParent.java

 /**
*
*/
package com.paic.reflection; /**
* @author Administrator
*
*/
public class ReflectionParent {
public void start() {
System.out.println("starting...");
} protected void eat() {
System.out.println("eating...");
} void end() {
System.out.println("ending...");
} @SuppressWarnings("unused")
private void sing() {
System.out.println("sing...");
}
}

2:定义子类ReflectionDemo1继承父类,定义两个自己的方法和两个测试方法

 /**
*
*/
package com.paic.reflection; import java.lang.reflect.Method; import org.junit.Test; /**
* @author Administrator
*
*/
public class ReflectionDemo1 extends ReflectionParent { @SuppressWarnings("unused")
private void read() {
System.out.println("reading...");
} public void write() {
System.out.println("writing...");
} /**
* @param args
*/
@Test
public void testGetMethods() {
Method[] methods = this.getClass().getMethods();
for (Method m : methods) {
System.out.println(m.getName());
}
} @Test
public void testGetDeclaredMethods() {
Method[] methods = this.getClass().getDeclaredMethods();
for (Method m : methods) {
System.out.println(m.getName());
}
} }

3:运行结果

a:运行testGetMethods()方法

Class类中getMethods() 与getDeclaredMethods() 方法的区别

b:运行testGetDeclaredMethods()方法

Class类中getMethods() 与getDeclaredMethods() 方法的区别

三:其他的方法,类似字段以及构造方法和方法类似

1:getFileds()与getDeclaredFileds()

2:getConstructors()与getDeclaredConstructors()

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858