Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 OOP新手,在类中管理变量_Java_Oop - Fatal编程技术网

Java OOP新手,在类中管理变量

Java OOP新手,在类中管理变量,java,oop,Java,Oop,所以。我从老师那里得到了一个任务:制作一个管理不同学生的程序。该项目将包含名称、教育、信息和积分 您可以选择: 增加新学生 改变教育和信息 管理积分 培养新学生不是问题,但管理教育、信息和特定学生的分数是困难的部分,这就是我需要你帮助的地方。My main.java目前不包含任何内容 Student.java package student; public class Student { String namn; String program; String inf

所以。我从老师那里得到了一个任务:制作一个管理不同学生的程序。该项目将包含名称、教育、信息和积分

您可以选择:

  • 增加新学生
  • 改变教育和信息
  • 管理积分
培养新学生不是问题,但管理教育、信息和特定学生的分数是困难的部分,这就是我需要你帮助的地方。My main.java目前不包含任何内容


Student.java

package student;
public class Student {
    String namn;
    String program;
    String info;
    int points;

public void managePoints(){

}
public void changeProgram(){

}
public void changeInfo(){

}

}

Main.java

package student;
public class main {
    public static void main(String[] args) {

    }
}

我不知道你到底想做什么,但你的
学生
课程(如果我认为你需要什么)应该更像这样:

public class Student {
    private String  name; // private because you don't want anyone to interact with the variable too much.
    private String  program;
    private String  info;
    private int     points;

    public Student( String name, String program, String info, int points ) { // contructor with variables to initialize. You can remove some of the variables if you do not consider they should be here.
        this.name = name;
        this.program = program;
        this.info = info;
        this.points = points;
        // without `this` you would change parameter's value to itself which isn't what you want.
    }

    public String getName( ) { // getter because I guess you would like to know students name
        return name;
    }

    public int getPoints( ) {
        return points;
    }

    public void addPoints( int points ) { // setter so you can modify points
        this.points += points;
    }

    public String getProgram( ) { // same as getName
        return program;
    }

    public void setProgram( String program ) {
        this.program = program;
    }

    public String getInfo( ) {
        return info;
    }

    public void setInfo( String info ) {
        this.info = info;
    }
}
但是如何使用这些方法呢?您可以使用它们,如下例所示

Student s1 = new Student("Abc Xyz", "IT", "Some informations", 12);
Student s2 = new Student("Cba Zyx", "Some other program", "Some more informations, 0);

s2.setInfo( s1.getInfo( ) );
s1.setPoints(1234);
s2.setProgram("Axbzcy");
Getter是一个返回(很可能)私有变量值的方法。
Setter是一种将私有变量的值设置为另一个值的方法,该值作为参数传递给该方法。

根据注释,我猜您的类中的三个方法应该将学生的点、程序和信息更改为所需的值。在Java中,我们称这些setter

您应该将方法重命名为
设定点
设置程序
设置信息
。你知道,这是一种模式

接下来,您将如何知道这些字段的“期望”值?你可能会说,我从方法中的文本框中获取它们。但更好的方法是从另一个方法获取值,并将值作为参数传递给setter

要向方法添加参数,请在方法声明的括号中添加类似thingy的变量:

public void setPoints (int p)
对于
setInfo

public void setInfo (String i)
等等

在方法体中,将字段设置为参数。例如,在您编写的
setInfo
方法中

info = i;
我想你可以找出其他的

现在你如何使用这些方法?例如,假设您有一个名为
student
的学生变量。您得到了他/她的信息,并将其存储在名为
studentInfo
的字符串变量中。您可以通过以下方式将学生变量的
info
设置为
studentInfo

student.setInfo (studentInfo);
或者,如果不想使用变量,可以使用字符串文字

student.setInfo("this is my info. Blah blah blah");
最终代码:

package student;
// The student class definition
public class Student {
   private String name;
   private String address;
   private String info;
   private String kurs;
   private int points;


   // Constructor
   public Student(String name, String address, String info, String kurs, int points) {
      this.name = name;
      this.address = address;
      this.points = points;
      this.kurs = kurs;
      this.info = info;

   }

   // Public getter for private variable name
   public String getName() {
      return name;
   }

   // Public getter for private variable address
   public String getAddress() {
      return address;
   }
      public String getInfo() {
      return info;
   }
         public int getPoints() {
      return points;
   }

   // Public setter for private variable address
   public void setAddress(String address) {
      this.address = address;
   }
   public void setPoints(int points){
       this.points = points;
   }
   public void setInfo(String info){
       this.info = info;
   }
   public void setKurs(String kurs){
       this.kurs = kurs;
   }

   // Describe itself
   public String toString() {
      return name + ", Adress: " + address + ", Info: " + info + ", Kurs: " + kurs + ", Poäng: " + points +" ";
   }

}
主要


谢谢大家的帮助。

这些方法应该做什么?提供更多信息。假设我们有一个新学生,得5分,教育是“TEINF”,信息是空的,并且是唯一的名称。例如,使用managePoints()将用所需的点覆盖当前点,与所有其他方法相同。用户将有一个输入框(文本框)输入新的值。您需要有更多的方法和实现来完成这些工作。保留一份学生名单。有他们的Id,该Id也将被索引。。等
package student;

// A test driver program for the Student class
public class main {
   public static void main(String[] args) {
      Student ArHa = new Student("A H", "Jysgaan 61", "ADHD", "Teknik", 5);
      ArHa.setPoints(10);
      ArHa.setKurs("TEINF");
     System.out.println(ArHa);
      Student DaSk = new Student("Dael Sklbr", "Fegea 65", "Svart", "Teknik", 5);
      DaSk.setInfo("Riktigt svart");
     System.out.println(DaSk);
      Student FaMe = new Student("Falafel Medusa", "Fågel 123", "Maten", "Kock", 123);
     System.out.println(FaMe);
   }
}