【问题标题】:Java Setters not working as intended on Array objectJava Setter 在 Array 对象上未按预期工作
【发布时间】:2020-08-31 00:03:24
【问题描述】:

我是 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 个对象)。

公共类主{

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 用于跟踪创建对象在数组中的哪个位置:

    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");
    

    }

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

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

    }

在方法之后我查找对象。名字和姓氏保持不变。但是,出生日期已根据需要进行了更改。

我错过了什么?

非常感谢 andvance!

【问题讨论】:

  • 您提供的值是什么? searchById 返回什么?你怎么说那个日期被改变了?
  • 顺便说一句,可怕的 Date 类在几年前被 java.time 类所取代。出于您的目的,请使用LocalDate

标签: java getter-setter


【解决方案1】:

您永远不会更改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;
}

【讨论】:

  • 太棒了,这就是我错过的。谢谢你的建议,这样更好!
猜你喜欢
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多