【问题标题】:Creating Methods and Classes (Java)创建方法和类 (Java)
【发布时间】:2016-01-28 23:00:18
【问题描述】:

我正在尝试编写一个名为 Student 的类,它应该与 StudentDriver 一起使用。但是,我很难理解方法和类的概念。我不知道如何接收和返回数据。此外,我什至不知道我是否正确声明了我的数据。请帮我。我将不胜感激。

另外,当我编译 Student 时,它说它找不到符号 this.setGPA。为何如此?在驱动程序中时,它具有 .setGPA。

谢谢。

// This will be the "driver" class for the Student class created in
// MinilabWritingClasses (It looks very complicated because of all
// the comments, but it really just creates instances of Student and
// tells them to do things...)

public class StudentDriver
{
public static void main(String[ ] args)
{
    //create an instance of Student
    System.out.println("***** Creating a Student, calling the default constructor");
    Student stud1 = new Student();

    //print it so we can see what the default values were for the class data
    //note that its toString() will be called automatically
    System.out.println("\n***** printing it - notice the default values (set by Java)");
    System.out.println(stud1);

    //create another instance of a Student, passing in initial values to its constructor
    System.out.println("\n***** Creating another Student, passing initial values to its constructor");
    Student msBoss = new Student("Bill Gates", 56, 'm', 3.2, true);

    //tell it to return its age
    System.out.println("\n***** telling it to return its age.");
    int theAge = msBoss.getAge();
    System.out.println("Its age is: " + theAge);

    //print it - note that its toString() will be called automatically;
    System.out.println("\n***** printing it - see if values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation
    System.out.println("\n***** asking it if it is on probation (check answer)");
    System.out.println("onProbation() returned: " + msBoss.onProbation());

    //tell it to change its gpa to 1.3
    System.out.println("\n***** telling it to change its gpa to 1.3");
    msBoss.setGPA(1.3);

    //print it now
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolean boolAnswer = msBoss.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);

    //tell it to complain
    System.out.println("\n***** telling it to complain");
    System.out.println("complain() returned: " + msBoss.complain());

    //tell it to change its onScholarship field to false
    System.out.println("\n***** telling it to change its onScholarship field to false");
    msBoss.setOnScholarship(false);

    //print it now
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(msBoss);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolAnswer = msBoss.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);

    //create a different student, tell it to have some different values, and tell it to print itself
    System.out.println("\n***** creating a different Student, passing initial values to its constructor");
    Student stud2;
    stud2 = new Student("Hillary Clinton", 64, 'f', 2.0, true);         //notice-can define variable and create it in 2 steps

    //print it
    System.out.println("\n***** printing it - see if the values are correct");
    System.out.println(stud2);

    //ask it if it is on probation now
    System.out.println("\n***** asking it if it is on probation (check answer)");
    boolAnswer = stud2.onProbation();
    System.out.println("onProbation() returned: " + boolAnswer);
 }
}

这是我正在写的课程。

public class Student
{
private String name;
private int age;
private char gender;
private double gpa;
private boolean onScholarship;

public Student()
{
}

public Student(String newName, int newAge, char newGender, double newGPA, boolean newScholarship)
{
    this.name = newName;
    this.age = newAge;
    this.gender = newGender;
    this.gpa = newGPA;
    this.onScholarship = newScholarship;
}

public int getAge(int newAge)
{
    return age;
}

public double setGPA (double newGPA)
{
    this.setGPA = newGPA;
}

public boolean setOnScholarship (boolean newScholarship)
{
    this.setOnScholarship = newScholarship;
}

public String toString()
{
    return this.name + "\t" + this.age + "\t" + this.gender + "\t" + this.setGPA + "\t" + this.setOnScholarship;
}

public boolean onProbation()
{
    if (onScholarship==true && gpa < 2.0)
        return true;
    else
        return false;
  }

}

【问题讨论】:

  • 什么是certain data?你能说得更具体点吗?
  • 几乎我的意思是所有这些,boolean、int、string、double、char 等。
  • 应该是this.gpa。另外,既然你不是从setter回来的,那就改成void
  • 你是说比尔盖茨只有3.2 GPA??!!我比那更好!
  • 谢谢。我现在看到它是如何工作的。哈哈,我的教授说的……不是我。

标签: java class object methods


【解决方案1】:

尝试改变这一行:

this.setGPA = newGPA;

到这里:

this.gpa = newGPA;

setGPA 找不到符号是因为没有setGPA 字段(它是一种方法)。您正在尝试更改 gpa 字段。

您也不需要空的 public Student() {} 构造函数——这是由 Java 自动创建的。

另外,正如@Sam 所指出的,由于setOnScholarship() 不返回任何内容,您可以将返回类型boolean 更改为void。这是因为没有return 语句,而这个returning of nothing 是void 类型。

不过,总的来说,您对创建另一个类的实例(即创建Students)有很好的了解。


根据要求(尽管它与您的代码没有太大关系),这里是static 的简短摘要。

static 关键字用于不与该类的实例一起使用的方法和字段,而是与该类本身一起使用。

例如,在您的情况下,几乎所有 Student 字段和方法都是非静态的,因为它们是 Student 对象的属性:

this.gpa;
this.setGpa();

另一方面,如果它没有更改与单个对象相关的变量,例如学生总数,您可以在 Student 中创建一个静态字段:

public class Student {

    // non-static fields for each instance
    public double gpa;

    // static field for the class
    public static numStudents;

    public Student() {
        // create student by setting object (non-static) fields, for example...
        this.gpa = 3.2;

        // edit static variable
        numStudents++; // notice how there is no `this` keyword - this changes a static variable
    }

}

...从StudentDrivernumStudents 可以通过以下方式检索:

Student.numStudents;  // notice how this is like `this.[property]`, but with the class name (Student) - it is an instance of the class

我希望这会有所帮助! OOP 编程是一个很难解释的话题。

【讨论】:

  • 谢谢。现在开始获得它。
  • @Paincakes 我很高兴它有帮助(不要忘记接受)!
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 2015-06-26
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
相关资源
最近更新 更多