簡體   English   中英

創建方法和類(Java)

[英]Creating Methods and Classes (Java)

我正在嘗試編寫一個名為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;
  }

}

嘗試更改此行:

this.setGPA = newGPA;

對此:

this.gpa = newGPA;

setGPA符號是因為沒有setGPA 字段 (這是一種方法)。 您正在嘗試更改gpa字段。

您也不需要空的public Student() {}構造函數-這是由Java自動創建的。

另外,正如@Sam指出的那樣,由於setOnScholarship()不返回任何內容,因此可以將返回類型boolean更改為void 這是因為沒有return語句,這return的什么ING是一個void的類型。

總體而言,您對創建另一個類的實例(即,創建Student )有很好的理解。


根據要求(盡管與您的代碼無關),這里是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可以用retreived:

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

我希望這有幫助! OOP編程是一個很難解決的話題,不能這么簡單地解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM