java - 创建方法和类 (Java)

标签 java class object methods

我正在尝试编写一个名为 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);
 }
}

这是我正在编写的类(class)。

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

}

最佳答案

尝试更改此行:

this.setGPA = newGPA;

对此:

this.gpa = newGPA;

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

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

此外,正如 @Sam 指出的,由于 setOnScholarship() 不返回任何内容,您可以将返回类型 boolean 更改为 void。这是因为没有 return 语句,而这种 return 什么也没有的类型是 void 类型。

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

<小时/>

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

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
    }

}

...并且从 StudentDriver 中,numStudents 可以通过以下方式检索:

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

我希望这有帮助! OOP 编程是一个困难的话题,不能这么简单地解释。

关于java - 创建方法和类 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403453/

相关文章:

java - 如何从 Java 中的 BigInteger 获取无符号字节数组?

java - 如何在给定方法中在一段时间内保持随机生成的数字相同?

python - 由exec(variable =)在for循环中分配的变量范围?

3 个对象中的 Javascript 求和值

java - 正则表达式\\s*,\\s*有什么作用?

java - 调整 Fragment 中 TextView 的文本大小

java - 是否有用于更好线性回归的 Java 库? (例如,迭代重新加权最小二乘法)

c++ - C++避免重复声明的语法是什么?

c++ - 模板类在 C++ 中生成错误

c++ - 创建采用可变数量的参数和数据类型的函数