简体   繁体   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');

  }
}

I'm trying to access the data in CreateList from a different .java file but my instructor wants me to do it in this specific way with private variables and sets and gets and I have no idea how to proceed. 我正在尝试从其他.java文件访问CreateList中的数据,但我的讲师希望我使用私有变量,集合和获取以这种特定方式进行操作,但我不知道如何进行。 I know how to create basic sets and gets but I don't understand how they're meant to work with the CourseInfo constructor. 我知道如何创建基本集合和获取,但是我不了解它们与CourseInfo构造函数一起使用的含义。

'Getters' and 'setters' are used to encapsulate variables / objects in Java. 'Getters'和'setters'用于封装Java中的变量/对象。 This means (as your instructor said), you should be making the variables private and the getters and setters public . 这意味着(如您的讲师所说),您应该将变量设为private,而将getters和setters设为public

For example: 例如:

private String courseTitle;

This variable is only accessible within the class, so to be able to get this variable from outside the class (ie from wherever you are using the CourseList from), you need to create a getter (to get the variable) and a setter (to change the value of the variable). 该变量只能在该类中访问,因此,要想从该类外部获取该变量(即,从使用CourseList任何位置),都需要创建一个getter (获取变量)和一个setter (以更改变量的值)。 As follows: 如下:

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

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

You can read more about encapsulation here . 您可以在此处阅读有关封装的更多信息。

Now, to retrieve the values, use the getter , as follows (assuming courseInfo is a CourseInfo object): 现在,要获取值,请使用getter ,如下所示(假设courseInfo是CourseInfo对象):

String data = courseInfo.getCourseTitle();

Try to do this for each variables create his set and get method like: private String courseTitle; 尝试为每个变量创建自己的set和get方法,例如:private String courseTitle;

 public String getCourseTitle() {
    return courseTitle;
}

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

And in CourseList class try to set the value of courseTitle like this: CourseList类中,尝试像这样设置courseTitle的值:

setCourseTitle("value of courseTitle");

Than you can get it like this: 比您可以像这样得到它:

String var = getCourseTitle();

And use the value where do you want for example add it in array courseList . 并使用您要在何处添加的值,例如,将其添加到courseList数组中。

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

相关问题 如何在类数组中分配元素 - How to Assign Elements in Class Array 如何从另一个类更改或分配给私有变量JTextField的值? - How do I Change or Assign a Value to a Private Variable JTextField from another class? 我应该将字符串导入类还是使用getString()将它们分配给变量? - Should I import strings into a class or use getString() to assign them to a variable? 尝试将值分配给数组元素并在另一个类中返回它以使用但不起作用 - Trying to assign value to array elements and return it in another class to work with but not working 如何将整数值分配给数组并将其传递给另一个函数? - How to assign integer values into array and pass them on into another function? 如何将数组变量的值分配给另一个数组变量 - How to assign value of an array variable into another array variable Java如何将数组中的变量分配给另一个数组 - Java How to assign variable from array to another array 如何使用在不同 class 中实现的接口来使用私有 class 变量 - How to use private class variable using interface implemented in a different class 如何使用 getter 函数访问另一个类中的私有变量? - How can access to a private variable in another class by using a getter function? 如何将数组列表的元素从另一个类复制到另一个类 - How to copy elements of array list from another class to another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM