您的位置:首页 > 编程语言 > Java开发

代码测试:java反射中getXXX和getDeclaredXXX的区别

2016-03-18 19:26 417 查看
原创不易,转载请注明出处,水平有限,有错漏敬请指出!

直接说结论,大伙有时间再看代码

一、结论:

1.用getXXX方法,只能取到自己和父类的public的属性或方法,其他都取不到。

2.用getDeclaredXXX方法,类里面定义的所有字段或方法,不论是什么修饰符,都能取到。

但不能取父类的任何字段。

二、注意:

1.Java是可以多级继承的,能取父类的public属性或方法,其实是能取它所有祖宗的public属性或方法的意思.

2.Java所有的类都继承了Object类,

所以,只要用了getXXX,就一定能取到Object类的public的方法.(Object类没有属性)

3.getDeclaredXXX能取到重写父类的方法

4.对于private的属性,有没有提供get,set方法跟用反射取值没关系,取不到就是取不到,提供了也没用。

三、代码思路

定义了一个Human类和Boy类,Boy类继承Human类,两个类都定义了用public,protect,default,private修饰

的方法和属性各1个,并各定义了1个XXXPrivateFieldWithGetSet属性,提供get,set方法。Human类重写了

Object类的toString()方法。然后打印通过getXXX和getDeclaredXXX取到的值,比较得出结论。

四、测试的源码

4.1 Human类

package com.cherrycheng.study.reflect.bean;

/**
* 人类
* 2016年3月18日
* @author chengru
*/
public class Human {
/** Human类的public属性*/
public String humanPublicField;
/** Human类的protected属性*/
protected int humanProtectedField;
/** Human类的default属性*/
String humanDefaultField;
/** Human类的private属性,提供getter,setter方法*/
private String humanPrivateFieldWithGetSet;
/** Human类的private属性,无getter,setter方法*/
private String humanPrivateFieldNoGetSet;

//----------------------- Override methods------------------------
@Override
public String toString() {
return "";
};

//----------------------- user-defined methods------------------------
public void humanPublicMethod(){
System.out.println("Human类的public方法");
}
protected void humanProtectedMethod(){
System.out.println("Human类的protected方法");
}
void humanDefaultMethod(){
System.out.println("Human类的default方法");
}
private void humanPrivateMethod(){
System.out.println("Human类的private方法");
}

//-----------------------gets and sets------------------------
public String getHumanPrivateFieldWithGetSet() {
return humanPrivateFieldWithGetSet;
}
public void setHumanPrivateFieldWithGetSet(String humanPrivateFieldWithGetSet) {
this.humanPrivateFieldWithGetSet = humanPrivateFieldWithGetSet;
}

}

4.2 Boy类

package com.cherrycheng.study.reflect.bean;

/**
* 男孩是人类的子类,继承人类的属性
* 2016年3月18日
* @author chengru
*/
public class Boy extends Human{
/** Boy类的public属性*/
public String boyPublicField;
/** Boy类的protected属性*/
protected int boyProtectedField;
/** Boy类的default属性*/
String boyDefaultField;
/** Boy类的private属性,提供getter,setter方法*/
private String boyPrivateFieldWithGetSet;
/** Boy类的private属性,无getter,setter方法*/
private String boyPrivateFieldNoGetSet;

//----------------------- user-defined methods------------------------
public void boyPublicMethod(){
System.out.println("Boy类的public方法");
}
protected void boyProtectedMethod(){
System.out.println("Boy类的protected方法");
}
void boyDefaultMethod(){
System.out.println("Boy类的default方法");
}
private void boyPrivateMethod(){
System.out.println("Boy类的private方法");
}

//-----------------------getters and setters------------------------
public String getBoyPrivateFieldWithGetSet() {
return boyPrivateFieldWithGetSet;
}
public void setBoyPrivateFieldWithGetSet(String boyPrivateFieldWithGetSet) {
this.boyPrivateFieldWithGetSet = boyPrivateFieldWithGetSet;
}
}


4.3 测试类

package com.cherrycheng.study.reflect.test;

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

import org.junit.Test;

import com.cherrycheng.study.reflect.bean.Boy;
import com.cherrycheng.study.reflect.bean.Human;

/**
* 测试有继承和没继承的类使用反射的getXXX和getDeclaredXXX的区别<br/>
* 2016年3月18日
* @author chengru
*/
public class DeclaredAndNot {
private void printFields(Class<?> cl){
int i=1;
System.out.println("------------"+cl.getName()+"'s Fields-----------");
for(Field f:cl.getFields()){
System.out.print(i+"、["+f.getName()+"] \t ");
++i;
}
System.out.println();

int j=1;
System.out.println("------------"+cl.getName()+"'s DeclaredFields-----------");
for(Field f:cl.getDeclaredFields()){
System.out.print(j+"、["+f.getName()+"] \t ");
++j;
}
System.out.println();
System.out.println();
}

private void printMethods(Class<?> cl){
int i=1;
System.out.println("------------"+cl.getName()+"'s Methods-----------");
for(Method f:cl.getMethods()){
System.out.print(i+"、["+f.getName()+"] \t ");
++i;
}
System.out.println();

int j=1;
System.out.println("------------"+cl.getName()+"'s DeclaredMethods-----------");
for(Method f:cl.getDeclaredMethods()){
System.out.print(j+"、["+f.getName()+"] \t ");
++j;
}
System.out.println();
System.out.println();
}

/**
* 测试继承的反射
* 2016年3月18日
* @author chengru
*/
@Test
public void TestInheritReflect(){
printFields(Boy.class);
printMethods(Boy.class);
}

/**
* 测试对自身的反射
* 2016年3月18日
* @author chengru
*/
@Test
public void TestSelfReflect(){
printFields(Human.class);
printMethods(Human.class);
}
}


五 运行结果

------------com.cherrycheng.study.reflect.bean.Boy's Fields-----------
1、[boyPublicField] 	 2、[humanPublicField]
------------com.cherrycheng.study.reflect.bean.Boy's DeclaredFields-----------
1、[boyPublicField] 	 2、[boyProtectedField] 	 3、[boyDefaultField] 	 4、[boyPrivateFieldWithGetSet] 	 5、[boyPrivateFieldNoGetSet]
------------com.cherrycheng.study.reflect.bean.Boy's Methods-----------
1、[boyPublicMethod] 	 2、[getBoyPrivateFieldWithGetSet] 	 3、[setBoyPrivateFieldWithGetSet] 	 4、[toString] 	 5、[humanPublicMethod] 	 6、[setHumanPrivateFieldWithGetSet] 	 7、[getHumanPrivateFieldWithGetSet] 	 8、[wait] 	 9、[wait] 	 10、[wait] 	 11、[equals] 	 12、[hashCode] 	 13、[getClass] 	 14、[notify] 	 15、[notifyAll]
------------com.cherrycheng.study.reflect.bean.Boy's DeclaredMethods-----------
1、[boyPublicMethod] 	 2、[boyProtectedMethod] 	 3、[boyDefaultMethod] 	 4、[boyPrivateMethod] 	 5、[getBoyPrivateFieldWithGetSet] 	 6、[setBoyPrivateFieldWithGetSet]
------------com.cherrycheng.study.reflect.bean.Human's Fields-----------
1、[humanPublicField]
------------com.cherrycheng.study.reflect.bean.Human's DeclaredFields-----------
1、[humanPublicField] 	 2、[humanProtectedField] 	 3、[humanDefaultField] 	 4、[humanPrivateFieldWithGetSet] 	 5、[humanPrivateFieldNoGetSet]
------------com.cherrycheng.study.reflect.bean.Human's Methods-----------
1、[toString] 	 2、[humanPublicMethod] 	 3、[setHumanPrivateFieldWithGetSet] 	 4、[getHumanPrivateFieldWithGetSet] 	 5、[wait] 	 6、[wait] 	 7、[wait] 	 8、[equals] 	 9、[hashCode] 	 10、[getClass] 	 11、[notify] 	 12、[notifyAll]
------------com.cherrycheng.study.reflect.bean.Human's DeclaredMethods-----------
1、[toString] 	 2、[humanPublicMethod] 	 3、[humanProtectedMethod] 	 4、[humanDefaultMethod] 	 5、[humanPrivateMethod] 	 6、[setHumanPrivateFieldWithGetSet] 	 7、[getHumanPrivateFieldWithGetSet]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: