您的位置:首页 > 其它

getDeclaredFields,getFields,getDeclaredMethods,getMethods实例测试!

2015-06-03 23:52 375 查看
今日学习Class类中几个常用的方法getDeclaredFields,getFields,getDeclaredMethod,getMethod。

接下来来看看他们都有什么用

1、getFields

返回的也是一个Field数组,数组中是当前类,其父类和所实现的接口中的public成员变量。(只有public的偶~~~)

2、getDeclaredFields

简明扼要的说就是返回一个Field数组,数组中是当前类中的成员变量。(public、protected、private和default)

3、getMethods

返回一个Method数组,数组中包含当前类、其父类和实现接口中的public方法。(同getFields,也是只有public的)

4、getDeclaredMethod

返回一个Method数组,数组中包含当前类中所有类型的方法(public、protected、private和default),不包含父类及接口中的方法。

测试代码中有四个类

Human,Animal,Student和ClassApiTest

//Human为父类
package reflect;
public class Human {
public String humanPublicVariable;
protected String humanProtectedVariable;
private String humanPrivateVariable;
String humanDefaultVariable;
public void humanPublicMethod(){
}

protected void humanProtectedMethod() {
}

private void humanPrivateMethod(){
}

void humanDefaultMethod(){
}
}

package reflect;
/**
* @author  作者 超
* @date 创建时间:2015年6月3日 下午11:42:11
* @version 1.0
* @description Animal为接口
*/
public interface Animal {
String animalInterfaceVariable = "";
void animalInterfaceMethod();
}

package reflect;
/**
* @author  作者 超
* @date 创建时间:2015年6月3日 下午11:01:32
* @version 1.0
* @description Student 是子类
*/
public class Student extends Human implements Animal{
public String studentPublicVariable;
protected String studentProtectedVariable;
private String studentPrivateVariable;
String studentDefaultVariable;
public void studentPublicMethod(){
}

protected void studentProtectedMethod() {
}

private void studentPrivateMethod(){
}

void studentDefaultMethod(){
}

@Override
public void animalInterfaceMethod() {
}
}

package reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* @author  作者 超
* @date 创建时间:2015年6月3日 下午11:07:11
* @version 1.0
* @description ClassApiTest类用于测试Class类中常用的api
* getDeclaredFields,getFields
*/
public class ClassApiTest {

public static void main(String[] args) {
Student stu = new Student();

System.out.println("\n----------测试getDeclaredFields方法---------\n"
+ "****getDeclaredFields方法用于获取当前类中所有变量****");
Field[] f = stu.getClass().getDeclaredFields();
for (Field field : f) {
System.out.println("field:"+field.toString());
}

System.out.println("\n----------测试getFields方法---------\n"
+ "****getFields方法用于获取当前类、父类及实现接口中的!!public!!变量****");
Field[] f1 = stu.getClass().getFields();
for (Field field : f1) {
System.out.println("field:"+field.toString());
}

System.out.println("\n----------测试getDeclaredMethods方法---------\n"
+ "****getDeclaredMethods方法用于获取当前类中所有方法****");
Method[] m = stu.getClass().getDeclaredMethods();
for (Method method : m) {
System.out.println("method:"+method.toString());
}

System.out.println("\n----------测试getMethods方法---------\n"
+ "****getMethods方法用于获取当前类、父类及实现接口中的!!public!!方法****");
Method[] m1 = stu.getClass().getMethods();
for (Method method : m1) {
System.out.println("method:"+method.toString());
}
}

}


测试结果如下图:





That‘s all~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  getMethods getFields api