熟悉的反射API

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;
	}
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值