OOP新手,管理类中的变量

时间:2015-11-18 12:55:23

标签: java oop

因此。我从老师那里得到一个任务,制作一个管理不同学生的程序。该计划将保留名称,教育,信息和要点。

您可以选择:

  • 添加新学生
  • 更改教育和信息
  • 管理点

培养新生不是问题,但为特定学生管理教育,信息和积分是困难的部分,这就是我需要你帮助的地方。我的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) {

    }
}

3 个答案:

答案 0 :(得分:1)

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

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是一种方法,它将私有变量的值设置为另一个值,该值作为参数传递给方法。

答案 1 :(得分:1)

根据评论,我猜你班上的三种方法应该将学生的分数,程序和信息改为所需的值。在Java中,我们称之为 setters

您应该将方法重命名为setPointssetProgramsetInfo。你知道,这是一种模式。

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

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

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");

答案 2 :(得分:0)

最终代码:

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

}

主要

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

谢谢大家的帮助。