泛型的典型应用:人的信息类和接口
2016-07-10 21:38
204 查看
interface Info{
}
class Contact implements Info{
private String address;
private String telephone;
private String email;
public Contact(String address, String telephone, String email) {
this.address = address;
this.telephone = telephone;
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "Contact [address=" + address + ", telephone=" + telephone
+ ", email=" + email + "]";
}
}
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "基本信息: [name=" + name + ", sex=" + sex + ", age=" + age
+ "]";
}
}
class Person<T extends Info>{
private T info;
public Person(T info){
this.setInfo(info);
}
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
}
public class Test {
public static void main(String[] args) {
Person<Contact> per = null;
per = new Person<Contact>(new Contact("北京","0100000001","111111"));
System.out.println(per.getInfo().toString());
Person<Introduction> per1 = null;
per1 = new Person<Introduction>(new Introduction("李四", "女", 24));
System.out.println(per1.getInfo().toString());
}
}
运行结果:
Contact [address=北京, telephone=0100000001, email=111111]
基本信息: [name=李四, sex=女, age=24]
}
class Contact implements Info{
private String address;
private String telephone;
private String email;
public Contact(String address, String telephone, String email) {
this.address = address;
this.telephone = telephone;
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "Contact [address=" + address + ", telephone=" + telephone
+ ", email=" + email + "]";
}
}
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "基本信息: [name=" + name + ", sex=" + sex + ", age=" + age
+ "]";
}
}
class Person<T extends Info>{
private T info;
public Person(T info){
this.setInfo(info);
}
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
}
public class Test {
public static void main(String[] args) {
Person<Contact> per = null;
per = new Person<Contact>(new Contact("北京","0100000001","111111"));
System.out.println(per.getInfo().toString());
Person<Introduction> per1 = null;
per1 = new Person<Introduction>(new Introduction("李四", "女", 24));
System.out.println(per1.getInfo().toString());
}
}
运行结果:
Contact [address=北京, telephone=0100000001, email=111111]
基本信息: [name=李四, sex=女, age=24]
相关文章推荐
- Java 之多线程同步
- 方便回顾篇进程
- 最小点覆盖 hdu--1054
- 最小点覆盖 hdu--1054
- 图片相似度比较--算法
- css
- android.os.NetworkOnMainThreadException 异常处理
- 存储过程使用DDL语句
- 算法导论(3) 快速排序、计数排序、基数排序
- Android开源项目 工具库
- Python 函数式编程--偏函数
- http://ftp.gnu.org/gnu/ http://ftp.gnu.org/gnu/libc/
- [APIO 2016] 赛艇 Boat:动态规划、离散化、组合数学和常数优化
- http://ftp.gnu.org/gnu/ http://ftp.gnu.org/gnu/libc/
- mysql 主从配置(master slave)
- java.lang.SecurityException: Permission denied (missing INTERNET permission?) 解决
- tv
- equals和==
- Python模拟登录
- VS调试C++项目实例