public class Student extends Person {
//id represents the student's ID
private int id;
//grade represents the student's grade in the course
private Grade grade;
//constructor allows user to define first and last names, id, and grade of student in demo
public Student(String fName, String lName, int id, Grade grade) {
super(fName, lName);
this.id=id;
this.grade=grade;
}
//get methods for fields
public int getId() {
return id;
}
public Grade getGrade() {
return grade;
}
//toString prints out the string from person class along with id and grade fields in formatted string
public String toString() {
return super.toString()+"'s id is " + id + "." +getGrade();
}
}
그리고이 코드가 두 가지 방법을 읽지 않습니다.
public class PassFailGrade extends Grade {
public PassFailGrade(double score) {
super(score);
}
public char getGrade() {
if (getScore()>=50)
return 'Y';
else
return 'N';
}
public String toString() {
return "(Y for yes/N for no) The student passed their course ("
+ getGrade()+ ")." + super.toString();
}
}
그런 데모 클래스 : PassFailGrade
에 문제가 있다면 문제는 toString 메소드는
public class Grade {
private double score;
public Grade(double score) {
this.score=score;
}
public void setScore(double score) {
this.score=score;
}
public double getScore() {
return score;
}
public char getGrade() {
if (getScore()>=90)
return 'A';
else if (getScore()>=80)
return 'B';
else if (getScore()>=70)
return 'C';
else if (getScore()>=60)
return 'D';
else
return 'F';
}
public String toString() {
return "\nThe student recieved a " + getGrade() +
" and had a mark of " + getScore() + ".";
}
}
확실하지 클래스 내에 위치한 방법 대신 passFailGrade getGrade() 반환 값을 사용 입니다 단지 생성자에서 정의하고 인쇄
public class StudentDemo {
public static void main(String[] args) {
PassFailGrade bo= new PassFailGrade(98);
Student s1 = new Student("bob", "blake", 123, bo);
System.out.println(s1);
}
}
출력 :
bob blake's id is 123.(Y for yes/N for no) The student passed their course (Y). The student recieved a Y and had a mark of 98.0.
당신은'PassFailGrade'클래스와'Student' 클래스라는 이름을 사용할 수 있습니까? – Apurv
학생 클래스는 이름을 정의하는 더 넓은 클래스이며 tostring에 getGrade()가 있습니다. – gucci
두 개의'getGrade' 메소드를 보았습니까? 어느 클래스에 속해 있습니까? – Apurv