Java设置方法在数组对象上无法正常工作

问题描述 投票:1回答:1

我对Java还是很陌生,想编写代码来创建对象,显示(打印)并对其进行编辑。

  1. 我写了一个课程:

    公共类人{

    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;
        }
    }
    

    }

  2. 我在主类中创建了一个人数组。 (100个对象)。

公共类Main {

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);
  1. 根据相应的命令,用户可以在此数组内创建一个对象(它是德语,但基本上是其读取的名字,姓氏和出生日期)。 mainCount用于跟踪创建对象在数组中的哪个位置:

    公共静态无效值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");
    

    }

  2. 现在的实际问题:调用以下方法,用户应该能够使用用户输入和设置器来编辑数组中所需的对象:

    public static void editPerson()引发ParseException {扫描仪扫描=新的Scanner(System.in);System.out.println(“ Bit ID der zu 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已根据需要进行了更改。

我想念的是什么?

非常感谢您!

java getter-setter
1个回答
2
投票

您永远都不会更改fullName,这是我认为您正在查看的。

this.fullName = this.firstName + " " + this.lastName;

您不需要存储它,每次计算起来都会更简单。

public String getFullName() {
    return this.firstName + " " + this.lastName;
}

BTW测试已经是布尔值了,所以您来写。

public boolean containsId(int id) {
    return this.id == id;
}
© www.soinside.com 2019 - 2024. All rights reserved.