【问题标题】:Accessing array attributes from other classes with setter/getter使用 setter/getter 从其他类访问数组属性
【发布时间】:2021-12-07 18:08:42
【问题描述】:

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

}

错误提示:Cannot invoke setDate(int[]) on the array type int[]

我该如何解决这个问题?谢谢!

【问题讨论】:

    标签: java class methods get set


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

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

        您可以使用this 作为参考

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 2020-04-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-25
            • 1970-01-01
            • 2015-08-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多