Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单独的类中访问私有变量?_Java_Arrays_Sorting_Object - Fatal编程技术网

Java 在单独的类中访问私有变量?

Java 在单独的类中访问私有变量?,java,arrays,sorting,object,Java,Arrays,Sorting,Object,这是家庭作业,所以我不希望你替我做 我有一个程序,根据学生ID号对学生进行排序。问题是,创建学生的学生类(由我的老师提供,不能更改)的ID设置为private,因此我无法在调用函数中访问它们 这是学生班: public class StudentQ { private String name; private int id; private double avg; StudentQ(String sname, int sid, double average){ name

这是家庭作业,所以我不希望你替我做

我有一个程序,根据学生ID号对学生进行排序。问题是,创建学生的学生类(由我的老师提供,不能更改)的ID设置为private,因此我无法在调用函数中访问它们

这是学生班:

public class StudentQ {
  private String name;
  private int id;
  private double avg;

  StudentQ(String sname, int sid, double average){
    name=sname;
    id=sid;
    avg=average;
  }

  // Note this uses the String static method format to produce a string
  // in the same way that System.out.printf does
  public String toString(){ 
    return String.format("%15s [%6d]:%7.2f", name,id,avg);
  }

  public String getName(){
    return name;
  }

  public int getID(){
    return id;
  }

  public double getAverage(){
    return avg;
  }

  public void setAverage(double newavg){
    avg=newavg;
  }
}
这是我的分类课:

static void sortByID(StudentQ[] students) {


  for (int lastPlace = students.length-1; lastPlace > 0; lastPlace--) {   
    int maxLoc = 0;
    for (int j = 1; j <= lastPlace; j++) {
      if (students[j].getID() > students[maxLoc].getID()) {
        maxLoc = j;  
      }
    }
    int temp = students[maxLoc].getID();
    *students[maxLoc].id = students[lastPlace].id;
    students[lastPlace].id= temp;*

  }

}
static void sortByID(StudentQ[]students){
对于(int lastPlace=students.length-1;lastPlace>0;lastPlace--){
int maxLoc=0;
对于(int j=1;j个学生[maxLoc].getID()){
maxLoc=j;
}
}
int temp=students[maxLoc].getID();
*学生[maxLoc].id=学生[lastPlace].id;
学生[lastPlace].id=temp*
}
}
现在的情况是,它告诉我field StudentQ.id不可见,并且我不能使用.getID(),因为它试图为方法赋值


谢谢。

使用
学生
类提供的
getID
方法,这就是它的用途(也称为getter)

您将遇到的下一个问题是,它没有setter…因此您不能(也不应该)分配ID的

相反,您应该交换实际的对象引用

StudentQ temp = students[maxLoc];
students[maxLoc] = students[lastPlace];
students[lastPlace] = temp;

使用
Student
类提供的
getID
方法,这就是它的用途(也称为getter)

您将遇到的下一个问题是,它没有setter…因此您不能(也不应该)分配ID的

相反,您应该交换实际的对象引用

StudentQ temp = students[maxLoc];
students[maxLoc] = students[lastPlace];
students[lastPlace] = temp;

Student类有一个ID变量的“getter”方法。对于需要由其他类访问的私有变量,这是一个常见的Java变通方法。调用
students[j].id
,而不是调用
students[j].getID()

Student类具有id变量的“getter”方法。对于需要由其他类访问的私有变量,这是一个常见的Java变通方法。调用
students[j].id
,而不是调用
students[j].getID()

您不应该为了对对象排序而更改学生id。如果你真的这样做了,你必须为变量定义一个“setter”(就像你已经拥有的getter),但是我的猜测是你真的应该重新考虑你的策略:排序对象通常不应该涉及改变对象本身!相反,移动对象本身,例如,为了对对象进行排序,您根本不必更改学生ID。如果你真的这样做了,你必须为变量定义一个“setter”(就像你已经拥有的getter),但是我的猜测是你真的应该重新考虑你的策略:排序对象通常不应该涉及改变对象本身!相反,移动对象本身,例如,
students[maxLoc]=students[lastPlace]

,我不能使用.getID(),因为它试图为方法赋值。你是什么意思?你为什么这么认为?我认为“that”指的是
students[maxLoc].id=students[lastPlace].id
部分,而不是getter。你可以使用Java集合排序方法吗?我不能使用.getID(),因为它试图为方法赋值。你是什么意思,为什么这么认为?我认为“that”指的是
students[maxLoc].id=students[lastPlace].id
part,而不是getter。是否允许使用Java集合排序方法?