Application of Generics
Keynote Teacher: Wang Shaohua QQ Group: 483773664
First, the demand
When designing a class, users tend to use the association of classes, for example, a person can define a property of information, but one may have a variety of information (such as contact, basic information, etc.), so the type of this information property can be declared by generics, and then as long as the appropriate information class is designed.
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/80/3B/wKioL1c786XBxvUmAAAxBTLUCT0317.jpg " data_ue_src= "E:\My knowledge\temp\7a4a9479-8859-4c1b-a80f-4c54f93e9b18.jpg" >
Second, reference code 1, interface: Info
12 |
public interface Info{ // 只有此接口的子类才是表示人的信息 } |
2. Contact information:
123456789101112131415161718192021222324252627282930313233 |
public class Contact
implements Info{
// 表示联系方式
private String address ;
// 联系地址
private String telephone ;
// 联系方式
private String zipcode ;
// 邮政编码
public Contact(String address,String telephone,String zipcode){
this
.address = address;
this
.telephone = telephone;
this
.zipcode = zipcode;
}
public void setAddress(String address){
this
.address = address ;
}
public void setTelephone(String telephone){
this
.telephone = telephone ;
}
public void setZipcode(String zipcode){
this
.zipcode = zipcode;
}
public String getAddress(){
return this
.address ;
}
public String getTelephone(){
return this
.telephone ;
}
public String getZipcode(){
return this
.zipcode;
}
@Override
public String toString() {
return "Contact [address=" + address +
", telephone=" + telephone
+
", zipcode=" + zipcode +
"]"
;
}
}
|
3, basic information: Introduction
123456789101112131415161718192021222324252627282930313233 |
public 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 ;
} Learn from teacher Wang Generics (10): Generics applications |