簡體   English   中英

擴展類不能正確構造; 雙重錯誤,但我不明白為什么

[英]Extended class doesn't construct properly; double error but I don't understand why

我有三個班級:兩個我不能接觸的班級-初始班級和顯示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;
   }
  }

在類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

暫無
暫無

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

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