我有三个班级:两个我不能碰的班级-初始班级和显示Demo班级-第三班是初始班级的扩展。
演示编译时,出现此错误:
EssayDemo.java:16: error: method setScore in class GradedActivity cannot be applied to given types;
termPaper.setScore(25.0, 18.0, 20.0, 25.0);
^
required: double
但是我所有的数据类型都设置为double,所以我不明白为什么会发生错误。我也无法更改演示代码。 setScore是在GradedActivity中创建的双精度型,但我也不能对此感到困惑。论文类中缺少或不正确的是什么?有人可以告诉我错误吗?
这是导致问题的essayDemo.java:
/**
This program demonstrates a solution to
the Essay Class programming challenge.
*/
public class EssayDemo
{
public static void main(String[] args)
{
// Create an Essay object.
Essay termPaper = new Essay();
// Assign scores to the object.
// Grammer = 25 points, Spelling = 18 points,
// Length = 20 points, and Content = 25 points.
termPaper.setScore(25.0, 18.0, 20.0, 25.0);
// Display the score details.
System.out.println("Term paper:");
System.out.println("Grammar points: " + termPaper.getGrammar());
System.out.println("Spelling points: " + termPaper.getSpelling());
System.out.println("Length points: " + termPaper.getCorrectLength());
System.out.println("Content points: " + termPaper.getContent());
System.out.println("Total points: " + termPaper.getScore());
System.out.println("Grade: " + termPaper.getGrade());
}
}
这是gradedActivity.java:
/**
The GradedActivity class stores data about a graded
activity for the Essay Class programming challenge.
*/
public class GradedActivity
{
private double score; // Numeric score
/**
The setScore method sets the score field.
@param s The value to store in score.
*/
public void setScore(double s)
{
score = s;
}
/**
The getScore method returns the score.
@return The value stored in the score field.
*/
public double getScore()
{
return score;
}
/**
The getGrade method returns a letter grade
determined from the score field.
@return The letter grade.
*/
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
这是我编写的扩展代码:
public class Essay extends GradedActivity{
private double grammar;
private double spelling;
private double correctLength;
private double content;
public Essay(){
grammar = 0;
spelling = 0;
correctLength = 0;
content = 0;
}
public Essay(double gramScore, double spelScore, double cLScore, double contScore){
grammar = gramScore;
spelling = spelScore;
correctLength = cLScore;
content = contScore;
}
double getGrammar(){
return grammar;
}
double getSpelling(){
return spelling;
}
double getCorrectLength(){
return correctLength;
}
double getContent(){
return content;
}
double getTotal(){
return grammar + spelling + correctLength + content;
}
}
答案 0 :(得分:3)
在您的课程Essay
中,您需要一种方法来重载父方法:
public void setScore(double gramScore, double spelScore, double cLScore, double contScore){
//your logic here
}
检查https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html