Java 设置器未按预期在数组对象上工作
Java Setters not working as intended on Array object
我对 Java 很陌生,想编写创建对象、显示(打印)和编辑对象的代码。
我写了一个class:
public class 人 {
protected String firstName;
protected String lastName;
protected String fullName;
protected Date dob;
protected int id;
public Person(String firstName, String lastName, Date dob, int id) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
this.fullName = this.firstName + " " + this.lastName;
this.id = id;
}
public String getFirstName() {return this.firstName;}
public String getLastName() {return this.lastName;}
public String getFullName() {
return this.fullName;
}
public Date getDob() {
return this.dob;
}
public int getId() {
return this.id;
}
public void printAll() {
System.out.println(this.fullName);
System.out.println(this.dob);
System.out.println(this.id);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setDob(Date dob) {
this.dob = dob;
}
public boolean containsFullName(String fullName) {
if (this.fullName.equalsIgnoreCase(fullName)) {
return true;
} else {
return false;
}
}
public boolean containsId(int id) {
if (this.id == id) {
return true;
} else {
return false;
}
}
}
我在 Main Class 中创建了一个人物数组。 (100 个对象)。
public class 主 {
final static int MAX_NUMBER_PERSONS = 100;
static Person[] person = new Person[MAX_NUMBER_PERSONS];
static int mainCount = 0;
static Scanner scan = new Scanner(System.in);
根据相应的命令,用户可以在此数组中创建一个对象(它是德语,但基本上是读名字、姓氏和出生日期)。 mainCount 用于跟踪创建对象在数组中的哪个位置:
public static void createPerson() 抛出 ParseException {
Scanner scan = new Scanner(System.in);
// Namen erfassen:
System.out.println("Bitte den Vornamen der Person eingeben:");
String firstName = scan.nextLine();
System.out.println("Bitte den Nachnamen der Person eingeben:");
String lastName = scan.nextLine();
String fullName = firstName + " " + lastName;
// DOB erfassen:
System.out.println("Bitte Geburtsdatum von " + fullName + " eingeben (TT/MM/JJJJ):");
String dob = scan.next();
Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob);
int id = mainCount;
person[mainCount] = new Person(firstName, lastName, dobDate, id);
++mainCount;
System.out.println("Du hast Person Nummer " + mainCount + " erstellt");
}
现在是实际问题:调用以下方法,用户应该能够使用用户输入和设置器在数组中编辑所需的对象:
public static void editPerson() 抛出 ParseException {
扫描仪扫描=新扫描仪(System.in);
System.out.println("Bitte die ID der zu bearbeitenden Person eingeben (zu finden über )...");
int id = scan.nextInt();
scan.nextLine();
searchByIdReturn(id).printAll();
System.out.println("Diese Person bearbeiten? Tippe <ja>, sonst zurück zum Menü.");
if(scan.next().equalsIgnoreCase("ja")) {
scan.nextLine();
System.out.println("Vorname eingeben...");
String firstName = scan.nextLine();
searchByIdReturn(id).setFirstName(firstName);
System.out.println("Nachname eingeben...");
String lastName = scan.nextLine();
searchByIdReturn(id).setLastName(lastName);
System.out.println("Bitte Geburtsdatum eingeben (TT/MM/JJJJ):");
String dob = scan.next();
Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob);
searchByIdReturn(id).setDob(dobDate);
}
}
在方法之后我查找了对象。名字和姓氏保持不变。但是,dob 已根据需要进行了更改。
我错过了什么?
万分感谢!
你永远不会改变 fullName
我假设你正在看的东西。
this.fullName = this.firstName + " " + this.lastName;
不需要存储,每次计算会更简单。
public String getFullName() {
return this.firstName + " " + this.lastName;
}
顺便说一句,测试已经是布尔值,所以你可以写。
public boolean containsId(int id) {
return this.id == id;
}
我对 Java 很陌生,想编写创建对象、显示(打印)和编辑对象的代码。
我写了一个class:
public class 人 {
protected String firstName; protected String lastName; protected String fullName; protected Date dob; protected int id; public Person(String firstName, String lastName, Date dob, int id) { this.firstName = firstName; this.lastName = lastName; this.dob = dob; this.fullName = this.firstName + " " + this.lastName; this.id = id; } public String getFirstName() {return this.firstName;} public String getLastName() {return this.lastName;} public String getFullName() { return this.fullName; } public Date getDob() { return this.dob; } public int getId() { return this.id; } public void printAll() { System.out.println(this.fullName); System.out.println(this.dob); System.out.println(this.id); } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setDob(Date dob) { this.dob = dob; } public boolean containsFullName(String fullName) { if (this.fullName.equalsIgnoreCase(fullName)) { return true; } else { return false; } } public boolean containsId(int id) { if (this.id == id) { return true; } else { return false; } }
}
我在 Main Class 中创建了一个人物数组。 (100 个对象)。
public class 主 {
final static int MAX_NUMBER_PERSONS = 100;
static Person[] person = new Person[MAX_NUMBER_PERSONS];
static int mainCount = 0;
static Scanner scan = new Scanner(System.in);
根据相应的命令,用户可以在此数组中创建一个对象(它是德语,但基本上是读名字、姓氏和出生日期)。 mainCount 用于跟踪创建对象在数组中的哪个位置:
public static void createPerson() 抛出 ParseException {
Scanner scan = new Scanner(System.in); // Namen erfassen: System.out.println("Bitte den Vornamen der Person eingeben:"); String firstName = scan.nextLine(); System.out.println("Bitte den Nachnamen der Person eingeben:"); String lastName = scan.nextLine(); String fullName = firstName + " " + lastName; // DOB erfassen: System.out.println("Bitte Geburtsdatum von " + fullName + " eingeben (TT/MM/JJJJ):"); String dob = scan.next(); Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob); int id = mainCount; person[mainCount] = new Person(firstName, lastName, dobDate, id); ++mainCount; System.out.println("Du hast Person Nummer " + mainCount + " erstellt");
}
现在是实际问题:调用以下方法,用户应该能够使用用户输入和设置器在数组中编辑所需的对象:
public static void editPerson() 抛出 ParseException { 扫描仪扫描=新扫描仪(System.in); System.out.println("Bitte die ID der zu bearbeitenden Person eingeben (zu finden über )..."); int id = scan.nextInt(); scan.nextLine(); searchByIdReturn(id).printAll();
System.out.println("Diese Person bearbeiten? Tippe <ja>, sonst zurück zum Menü."); if(scan.next().equalsIgnoreCase("ja")) { scan.nextLine(); System.out.println("Vorname eingeben..."); String firstName = scan.nextLine(); searchByIdReturn(id).setFirstName(firstName); System.out.println("Nachname eingeben..."); String lastName = scan.nextLine(); searchByIdReturn(id).setLastName(lastName); System.out.println("Bitte Geburtsdatum eingeben (TT/MM/JJJJ):"); String dob = scan.next(); Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob); searchByIdReturn(id).setDob(dobDate); }
}
在方法之后我查找了对象。名字和姓氏保持不变。但是,dob 已根据需要进行了更改。
我错过了什么?
万分感谢!
你永远不会改变 fullName
我假设你正在看的东西。
this.fullName = this.firstName + " " + this.lastName;
不需要存储,每次计算会更简单。
public String getFullName() {
return this.firstName + " " + this.lastName;
}
顺便说一句,测试已经是布尔值,所以你可以写。
public boolean containsId(int id) {
return this.id == id;
}