package com.jevon.crm.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
import com.jevon.crm.domain.Contact;
import com.jevon.crm.domain.Industry;
import com.jevon.crm.domain.Region;
public class ReflectTest {
@Test
public void test() throws SecurityException, NoSuchFieldException, NoSuchMethodException {
Enterprise enterprise = new Enterprise();
Class<? extends Enterprise> clazz = enterprise.getClass();
/** Class的关于Field的测试 */
// 只返回自己所有的字段,不包括父类
Field[] fields = clazz.getDeclaredFields();
// 只返回自己有的指定字段,不包括父类
// Field field = clazz.getDeclaredField("contact");
// 返回所有带public的字段,包括父类
// Field[] fields = clazz.getFields();
// 返回指定的public的字段,包括父类
// Field field = clazz.getField("name");
// System.out.println(field.getName());
for(Field f:fields){
System.out.println(f.getName());
}
/** Class的关于Method的测试 */
// 只返回自己所有的方法,不包括父类
// Method[] methods = clazz.getDeclaredMethods();
// 只返回自己有的指定字段,不包括父类
// Method method = clazz.getDeclaredMethod("getContact",new Class[]{});
//返回自己指定的public的方法,包括父类
// Method method = clazz.getMethod("getContact", new Class[]{});
//返回自己所有带public的方法,包括父类
// Method[] methods = clazz.getMethods();
// System.out.println(method.getName());
// for(Method m:methods){
// System.out.println(m.getName());
// }
}
}
/**
*
* @author Jevon
* createTime:2011-9-20 下午02:45:58
* description:父类Customer
*/
class Customer {
private Long id;
private String name;
private String address;
private String postcode;
private String identityCard;
private Industry industry;
private Region region;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getIdentityCard() {
return identityCard;
}
public void setIdentityCard(String identityCard) {
this.identityCard = identityCard;
}
public Industry getIndustry() {
return industry;
}
public void setIndustry(Industry industry) {
this.industry = industry;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
}
/**
* 子类Enterprise
* @author Jevon
* createTime:2011-9-20 下午02:45:31
* description:
*/
class Enterprise extends Customer {
private int level;//(采用数字表示,0 代表地市级、1 代表省级、2 代表全国级)
private String environment;
private String relation;//采用字符串表示,winwin 代表买断型、buyout 代表合作双赢型
private String account;
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
}
熟悉的反射API
最新推荐文章于 2022-04-19 19:40:54 发布