创建方法和类(Java)

创建方法和类(Java),java,class,object,methods,Java,Class,Object,Methods,我正在尝试写一个名为Student的类,它应该与StudentDriver一起工作。然而,我很难理解方法和类的概念。我不知道如何接收和返回数据。此外,我甚至不知道我是否正确声明了数据。请帮帮我。我将不胜感激 另外,当我编译学生时,它说它找不到符号this.setGPA。为什么呢当在驱动程序中时,它具有.setGPA 多谢各位 // This will be the "driver" class for the Student class created in // MinilabWritingC

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

另外,当我编译学生时,它说它找不到符号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;
  }

}
公共班级学生
{
私有字符串名称;
私人互联网;
私人性别;
私人双gpa;
私用布尔运算;
公立学生()
{
}
公立学生(字符串newName、int newAge、char newGender、double newGPA、boolean newScholarship)
{
this.name=newName;
这个年龄=新年龄;
this.gender=newGender;
this.gpa=newGPA;
this.oncholarship=新闻cholarship;
}
公共整数获取(整数更新)
{
回归年龄;
}
公共双设置GPA(双新GPA)
{
this.setGPA=newGPA;
}
公共布尔设置CHOLARSHIP(布尔新CHOLARSHIP)
{
this.setOnScholarship=newScholarship;
}
公共字符串toString()
{
返回this.name+“\t”+this.age+“\t”+this.gender+“\t”+this.setGPA+“\t”+this.setOnScholarship;
}
公共布尔值()
{
如果(onScholarship==true&&gpa<2.0)
返回true;
其他的
返回false;
}
}

尝试更改此行:

this.setGPA = newGPA;
为此:

this.gpa = newGPA;
setGPA
符号未找到是因为没有
setGPA
字段(这是一种方法)。您正试图更改
gpa
字段

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

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

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


根据要求(尽管这与您的代码没有太大关系),下面是关于
静态
的简要总结

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
numstudent
可以通过以下方式检索:

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

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

什么是
特定数据
?你能说得更具体一点吗?我指的几乎是所有的,布尔、int、string、double、char等等。应该是
this.gpa
。另外,由于您不是从
setter
返回,所以将其设为
void
您是说比尔盖茨的平均成绩只有3.2分吗??!!我比那更好!非常感谢。我现在明白了。哈哈,我的教授说。。。不是我,谢谢。现在开始买了。@Paincakes我很高兴它有帮助(别忘了接受)!