简体   繁体   English

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

[英]Accessing a private variable in a separate class?

This is homework, so I don't expect you to do it for me. 这是家庭作业,所以我不希望您为我做。

I have a program that sorts students by their student ID numbers. 我有一个程序,可以按学生的学生证号对学生进行排序。 The problem is that the student class that creates the students (provided by my teacher, can't change) has the ID's set as private, so I can't access them in the calling function. 问题在于,创建学生的学生班级(由我的老师提供,不能更改)将ID设置为私有,因此我无法在调用函数中访问他们。

This is the student class: 这是学生班:

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;
  }
}

and this is my sorting class: 这是我的分类课:

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;*

  }

}

The way it is now, it gives me and error saying that the field StudentQ.id is not visable, and I can't use .getID() because that is trying to assign a value to a method. 现在的样子,这给了我一个错误,并显示错误消息:StudentQ.id字段不可见,并且我无法使用.getID(),因为它试图将值分配给方法。

Thank you. 谢谢。

Use the supplied getID method of the Student class, that's what it's there for (AKA a getter) 使用Student类提供的getID方法,这就是它的用途(又称getter)

students[lastPlace].getID();

The next problem you will have is the fact that there is no setter for it...so you can't (and shouldn't) assign the ID's 您将遇到的下一个问题是没有设置程序的事实……因此您不能(也不应该)分配ID的

Instead, you should swap the actual object references... 相反,您应该交换实际的对象引用...

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

The Student class has a "getter" method for the ID variable. Student类具有用于ID变量的“获取器”方法。 This is a common Java workaround for private variables that need to be accessed by other classes. 这是需要其他类访问的私有变量的常见Java解决方法。 Instead of students[j].id , call students[j].getID() 代替students[j].id ,请打电话给students[j].getID()

You shouldn't have to change the student IDs at all, in order to sort the objects. 您无需完全更改学生ID,即可对对象进行排序。 If you truly do, you must define a "setter" for the variable (like the getter that you already have), but my guess is that really you should be rethinking your strategy: sorting objects generally should not involve altering the objects themselves! 如果你真的这样做,你必须定义的变量“二传手”(如,吸气,你已经有了),但我的猜测是真的,你应该重新考虑你的策略:一般分拣对象应该涉及改变物体本身! Instead, move the objects themselves , eg students[maxLoc] = students[lastPlace] . 而是自己移动对象 ,例如, students[maxLoc] = students[lastPlace]

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

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