0

我是 Java 新手,想知道如果它们是数组,如何使用 setter/getter 访问其他类的属性。

目前,我有一个日期类,它设置带有月/日/年参数的日期。我需要创建另一个类,该类使用日期类来设置雇用日期以存储为属性,以及其他类。

我的代码目前如下所示:

public class DateClass {
    
    private int month;
    private int day;
    private int year;

    // MONTH
    public int getMonth() {
        return month;
    }
    
    public void setMonth(int month) {
        this.month = month;
    }
    
    // DAY
    public int getDay() {
        return day;
    }
    
    public void setDay(int day) {
        this.day = day;
    }
    
    // YEAR
    public int getYear() {
        return year;
    }
    
    public void setYear(int year) {
        this.year = year;
    }
    
    // FULL DATE
    public void setDate(int month, int day, int year) {
        setMonth(month);
        setDay(day);
        setYear(year);
    }
    
    public int[] getDate() {
        return new int[] { month, day, year };
        
    }

}

这是第二类:

public class EmployeeClass {

private int[] dateOfHire;

public void setDateOfHire(int[] dateOfHire) {
    dateOfHire.setDate(dateOfHire);
}

public String[] getDateOfHire() {
    return dateOfHire.getDate(dateOfHire);
}

}

错误说:无法在数组类型 int[] 上调用 setDate(int[])

我怎样才能解决这个问题?谢谢!

4

4 回答 4

1

你需要做一些改变。首先,您EmployeeClass应该有一个DateClass属性,而不是int[]setDateOfHire()方法必须调用具有 3 个参数setDate()的可用方法:DateClass

public class EmployeeClass {
    private DateClass dateOfHire;

    public void setDateOfHire(int[] dateOfHire) {
        this.dateOfHire.setDate(dateOfHire[0], dateOfHire[1], dateOfHire[2]);
    }

    public int[] getDateOfHire() {
        return dateOfHire.getDate();
    }
}

这会起作用,但我建议你检查你的课程设计。让int数组定义日期是一种非常糟糕的做法。

于 2021-10-20T23:40:17.227 回答
0

您正在声明一个整数基元数组private int[] dateOfHire;

DateClass声明一个简单使用的数组private DateClass[] dateOfHire

之前定义的原语或类[]决定了初始化数组的类型

您可以将此 作为参考

于 2021-10-20T23:41:26.460 回答
0

您将需要创建一个 DateClass 对象来使用它的方法。考虑像这样更改您的 EmployeeClass:

public class EmployeeClass {
    
private int[] dateOfHire; // not sure of the purpose of this array
private DateClass dateClassObject; // your variable name is up to you


public EmployeeClass () { // Constructor for this class

    dateClassObject = new DateClass(); // created a new DateClass in the constructor

}

public void setDateOfHire(int[] dateOfHire) {
        // this dateOfHire array is the array that is passed in when
        // method is called. Do not confuse it the array declared as a property.
        int day = dateOfHire[0] // assuming the day is in index 0 of the array
        int month = dateOfHire[1] // assuming the month is in index 1 of the array
        int year = dateOfHire[2] // assuming the year is in index 2 of the array
        dateClassObject.setDate(month, day, year);  // changed to dateClassObject. Also changed setDate to pass correct parameters
    }
    
public int[] getDateOfHire() { // changed String[] to int[] because returned value of getDate() is a int array.
        return dateClassObject.getDate(); // changed to dateClassObject. Removed the parameter.
}
    
}
于 2021-10-20T23:52:00.227 回答
0

只需设置值而setDateOfHire不是对其调用方法:

public void setDateOfHire(int[] dateOfHire) {
    this.dateOfHire = dateOfHire;
}

将其与您在 DateClass 中实现设置器的方式进行比较。

于 2021-10-20T23:56:10.147 回答