繁体   English   中英

使用 setter/getter 从其他类访问数组属性

[英]Accessing array attributes from other classes with setter/getter

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

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

我的代码目前看起来像这样:

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[])

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

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

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数组定义日期是一种非常糟糕的做法。

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

要声明一个DateClass数组,只需使用private DateClass[] dateOfHire

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

您可以将此作为参考

您需要创建一个 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.
}
    
}

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

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

将它与您在 DateClass 中实现 setter 的方式进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM