Ich bin neu in Java.
Dieses Mal habe ich eine Klasse namens Information erstellt und darin ** ・ Adresse [String] ** ** ・ Telefon (Telefonnummer) [String] ** Ich habe ein Feld namens gemacht. Zusätzlich haben wir für jeden Artikel Getter und Setter vorbereitet.
Um eine Kontaktsuchfunktion zu erstellen, erstellen Sie als Nächstes eine Personenklasse und darin ** ・ name [String] ** ** ・ Geburtstag [int] ** ** ・ Informationen [Informationen] ** Ich habe ein Feld namens erstellt, den Namen, den Geburtstag und die Informationen nach dem Faktor des Konstruktors festgelegt und jeden Getter vorbereitet.
Als Nächstes habe ich versucht, der Informationsklasse die folgenden drei Methoden hinzuzufügen. ** ・ toString () ** ** ・ entspricht () ** ** · Hash-Code () **
Wenn die Informationen zu diesem Zeitpunkt korrekt sind, bedeutet dies, dass Adresse und Telefonnummer übereinstimmen. Außerdem gibt toString () jetzt alle Felder als Zeichenfolgen zurück.
toString () und equals () wurden irgendwie abgeschlossen, basierend auf Informationen im Netz. Ich habe jedoch Probleme, weil ich Hashcode () nicht verstehe.
Der Code, den ich jetzt geschrieben habe, ist unten. Ich denke, es gibt viele Punkte, die außer hashcode () nicht erreicht werden können. Ich wäre Ihnen dankbar, wenn Sie auch diese Punkte kommentieren könnten.
Person.class
public class Person {
private String name;
private int birthday;
private Information information;
public Person(String name, int birthday, Information information) {
this.name = name;
this.birthday = birthday;
this.information = information;
}
public String getName() {
return this.name;
}
public int getBirthday() {
return this.birthday;
}
public Information getInformation() {
return this.information;
}
}
Information.class
public class Information {
private String address;
private String phone;
public Information() {}
public String getAddress() {
return this.address;
}
public String getPhone() {
return this.phone;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString(){
String str = address+","+phone;
return str;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Information)) {
return false; //
}
Information otherInformation = (Information) other;
if ((this.address == otherInformation.getAddress()) && (this.phone == otherInformation.getPhone())){
return true;
}
return false;
}
public int hashcode(){
//Ich weiß es nicht.
}
}
Recommended Posts