gpt4 book ai didi

java - 创建方法和类 (Java)

转载 作者:行者123 更新时间:2023-11-30 03:12:11 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com