泛型是在jdk
view plain
package org
public interface Info {
}
view plain
package org
import org
public class Introduction implements Info {
private String name;
private String age;
public Introduction(String name
this
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getAge() {
return age;
}
public void setAge(String age) {
this
}
public String toString() {
return
}
view plain
package org
import org
public class Contact implements Info {
private String address;
private String phone;
public String getAddress() {
return address;
}
public Contact(String address
this
this
}
public void setAddress(String address) {
this
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this
}
public String toString() {
return
}
}
這裡這麼寫就代表人的屬性
view plain
package org
import org
public class Person<T extends Info> {
private T info;
public Person(T info){ //在這裡Person可以接收Contact也可以接收Introduction
this
}
public void setInfo(T info) {
= info;
}
public T getInfo() {
return info;
}
public String toString() {
return
}
}
view plain
package org
import org
import org
import org
public class Test {
public static void main(String args[]){
Person<Introduction> p=new Person<Introduction>(new Introduction(
System
Person<Contact> p
System
}
}
這個測試類把聯系方式和基本信息進行了顯示
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26755.html