Java泛型与接口的应用示例

代码如下

// 公共接口,只有实现这个接口的子类才表示人的信息.
interface Info{
	
}

// 联系方式类
class Contact implements Info{
	private String addr;				// 地址
	private String telphone;		// 电话
	private String zipcode;			// 邮编
	
	// 构造函数
	public Contact(String addr, String tel, String code){
		this.addr = addr;
		this.telphone = tel; 	
		this.zipcode = code;
	}	
	
	// 设置地址
	public void setAddress(String addr){
		this.addr = addr;	
	}
	
	// 设置电话
	public void setTelphone(String tel){
		this.telphone = tel;	
	}
	
	// 设置邮编
	public void setZipcode(String code){
		this.zipcode = code;
	}
	
	// 获取地址
	public String getAddress(){
		return this.addr;	
	}
	
	// 获取电话
	public String getTelphone(){
		return this.telphone;	
	}
	
	// 获取邮编
	public String getZipcode(){
		return this.zipcode;	
	}
	
	// 将对象格式化成字符串
	public String toString(){
		return "\t\t\t****联系方式****\n" + "联系地址: " + this.getAddress() + 
						"\t联系电话: "	+ this.getTelphone() + 
						"\t邮编: " + this.getZipcode();
	}
}


// 基本信息类
class Introduction implements Info{
	private String name;				// 姓名
	private String sex;					// 性别
	private int age;						// 年龄
	
	// 基本信息构造函数
	public Introduction(String name, String sex, int age){
		this.name = name;
		this.sex = sex; 	
		this.age = age;
	}	
	
	// 设置名字
	public void setName(String name){
		this.name = name;	
	}
	
	// 设置性别
	public void setSex(String sex){
		this.sex = sex;	
	}
	
	// 设置年龄
	public void setAge(int age){
		this.age = age;
	}
	
	// 获取姓名
	public String getName(){
		return this.name;	
	}
	
	// 获取性别
	public String getSex(){
		return this.sex;	
	}
	
	// 获取年龄
	public int getAge(){
		return this.age;	
	}
	
	// 将对象格式化成字符串
	public String toString(){
		return "\t\t\t****基本信息****\n" + "姓名: " + this.getName() + 
						"\t性别: "	+ this.getSex() + 
						"\t年龄: " + this.getAge();
	}
}

// 泛型类Person,泛型参数类型必须是Info或者实现Info接口的子类.
class Person<T extends Info>{
	
	private T info;							// 声明T类型的info对象.	T必须是Info或者实现Info接口的子类.
	
	// 构造函数,参数为T类型,表示信息的类型
	public Person(T info){
		this.info = info;	
	}
	
	// 设置人的信息类型T
	public void setInfo(T info){
		this.info = info;	
	}
	
	// 获取人的信息
	public T getInfo(){
		return this.info;	
	}
	
	// 将Person的对象格式化成字符串
	public String toString(){
		return this.info.toString();				// 调用相应类型的info的toString()函数,将格式化成对应的格式
	}
}

// main
public class GenericUseCase{
	public static void main(String args[]){
		// 实例化Person类对象,泛型参数为Contact类型,该类型实现了Info接口,所有符合Person的参数类型<T extends Info>
		Person<Contact> per01 = new Person<Contact>(new Contact("桂林市","10086","116600"));	
		System.out.println( per01 + "\n");
		
		// 实例化Person类对象,泛型参数为Introduction类型,该类型实现了Info接口,所有符合Person的参数类型<T extends Info>
		Person<Introduction> per02 = new Person<Introduction>(new Introduction("高富帅","男",21));	
		System.out.println( per02 );
	}	
}

输出结果:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值