簡體   English   中英

如何將數組元素分配給私有變量並在另一個類中使用它們?

[英]How to assign array elements to private variable and use them in another class?

/*
c - csis
a - acct
b - busn
p - phys

*/
class CourseInfo {

  private String courseTitle;
  private double coursePrice;
  private int courseSeatsAvail;
  private String courseDesc;
  private char courseType;

  private static String empPassword;

  CourseInfo(String crseTitle, double crsePrice, int crseSeats, String crseDesc, char type) {

    courseTitle = crseTitle;
    coursePrice = crsePrice;
    courseSeatsAvail = crseSeats;
    courseDesc = crseDesc;
    courseType = type;
  }

  /*
   * Finish the class with the set/get methods
   */
}

///////////////////////////////

class CourseList {

  CourseInfo[] courseList;

  public void createList() {

    courseList = new CourseInfo[11];

    courseList[0] = new CourseInfo("acct110", 375.49, 35, "this course teaches \nbasic accounting practice", 'a');
    courseList[1] = new CourseInfo("busn110", 375.49, 35, "this course teaches \nbasic business pratice", 'b');
    courseList[2] = new CourseInfo("busn240", 375.49, 35, "this course teaches \nadvance business pratice", 'b');
    courseList[3] = new CourseInfo("csis110", 375.49, 2, "this course teaches \nbasic computing pratice", 'c');
    courseList[4] = new CourseInfo("csis220", 375.49, 35, "this course teaches \nR language", 'c');
    courseList[5] = new CourseInfo("csis290", 375.49, 25, "this course teaches \nbasic hardware pratice", 'c');
    courseList[6] = new CourseInfo("csis340", 375.49, 35, "this course teaches \nadvance CPU tech", 'c');
    courseList[7] = new CourseInfo("csis420", 375.49, 17, "this course teaches \nbasic computer graphics", 'c');
    courseList[8] = new CourseInfo("csis491", 375.49, 3, "this course teaches \nbasic game programming", 'c');
    courseList[9] = new CourseInfo("phys120", 499.19, 30, "this course teaches \nbasic physics theory", 'p');
    courseList[10] = new CourseInfo("phys240", 399.99, 35, "this course teaches \nbasic quantum mechanics", 'p');

  }
}

我正在嘗試從其他.java文件訪問CreateList中的數據,但我的講師希望我使用私有變量,集合和獲取以這種特定方式進行操作,但我不知道如何進行。 我知道如何創建基本集合和獲取,但是我不了解它們與CourseInfo構造函數一起使用的含義。

'Getters'和'setters'用於封裝Java中的變量/對象。 這意味着(如您的講師所說),您應該將變量設為private,而將getters和setters設為public

例如:

private String courseTitle;

該變量只能在該類中訪問,因此,要想從該類外部獲取該變量(即,從使用CourseList任何位置),都需要創建一個getter (獲取變量)和一個setter (以更改變量的值)。 如下:

// Getter
public String getCourseTitle() {
    return this.courseTitle;
}

// Setter
public void setCourseTitle(String newCourseTitle) {
    this.courseTitle = newCourseTitle;
}

您可以在此處閱讀有關封裝的更多信息。

現在,要獲取值,請使用getter ,如下所示(假設courseInfo是CourseInfo對象):

String data = courseInfo.getCourseTitle();

嘗試為每個變量創建自己的set和get方法,例如:private String courseTitle;

 public String getCourseTitle() {
    return courseTitle;
}

public void setCourseTitle(String courseTitle) {
    this.courseTitle = courseTitle;
}

CourseList類中,嘗試像這樣設置courseTitle的值:

setCourseTitle("value of courseTitle");

比您可以像這樣得到它:

String var = getCourseTitle();

並使用您要在何處添加的值,例如,將其添加到courseList數組中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM