Author: Zhi GUI
Address: i8n. Cn/WcLnBo
Create an interface, three classes, to achieve the integration of a person’s basic information. (Practical use of generics)
First define the identity interface — the information
public interface Info {}Copy the code
The first class implements contact information
public class Contact implements Info {
private String address;
private String telphone;
private String zipcode;
public Contact(String address,String telphone,String zipcode) {
this.address = address;
this.telphone = telphone;
this.zipcode = zipcode;
}
public String getaddress(a) {
return address;
}
public String gettelphone(a) {
return telphone;
}
public String getzipcpde(a) {
return zipcode;
}
public String toString(a) {
return "Contact Information:"+"\n"+"\ t | - contact phone number."+this.telphone+
"\ t | - contact address:"+this.address+
"\ t | - zip code."+this.zipcode; }}Copy the code
Ps: Finally, toString overwrite is used for the final output
The second class implements basic personal information
public class Intro implements Info { private String name; private String sex; private int age; public Intro(String name,String sex,int age) { this.name = name; this.sex = sex; this.age = age; } public String getname() { return name; } public String getsex() { return sex; } public int getage() { return age; } public String toString () {return "basic information:" + "\ n" + "\ t | - name:" + this. The name + "\ t | - gender:" + this. Sex + "\ t | - age:" + enclosing the age; }}Copy the code
Ps: In fact, there is no big difference between these two classes, only the parameters are changed. It's all about setting the value of the property using the constructor and then getting the value using the GET method. I'm not a fan, so I'll use this for now.
Finally, define a Person class
The type of the info attribute is generic
public class Person<T extends Info> {
private T info;
public Person(T info) {
this.info = info;
}
public T getinfo(a) {
return info;
}
public String toString(a) {
return this.info.toString(); }}Copy the code
Ps: You can clearly see that the properties and methods in this class use generics. Set an upper limit for generics, specifying that they can only be subclasses of the Info interface, and then proceed to set and get the final value using the constructor and get methods.
Note that the toString method finally calls the toString method of the Info interface subclass, overwriting the toString method of the Object class.
And finally, the pivot function
Instantiate two objects to implement different functions
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String a1 = in.next();
String a2 = in.next();
String a3 = in.next();
Person <Contact> a = new Person<Contact>(new Contact(a1,a2,a3));
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
System.out.println(a);
String b1 = in.next();
String b2 = in.next();
int b3 = in.nextInt();
Person <Intro> b = new Person<Intro>(newIntro(b1,b2,b3)); System.out.println(b); }}Copy the code
Output:
13412345678Chongqing banan402300-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- contact: | - contact phone number: chongqing banan | - contact address:13412345678| - zip code:402300O the newsletter male18Basic information: | - name: the rapid | - gender: male | - age:18
Copy the code
Ps: This is where the beauty of generics comes in. It would be annoying not to use generic conversions for properties of different types, but here you just declare the generic as a subclass of Info, and the entire property will change as the subclass changes.
In particular, the Person object must be instantiated as well as the object of the Info subclass. This subclass must be instantiated in order to use a method. This can be hard to notice sometimes.
Hundreds of popular e-books for free:Github.com/XiangLinPro…