java - Eclipse 拒绝我的公共(public) void init 方法

Eclipse SDK v3.2.1 拒绝我的 公共(public)无效初始化 方法。
我正在使用以下导入:

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

我的程序有一个 run() 方法和一个 init() 方法,但是 init() 导致了这些错误
- overrides acm.program.Program.init
- Cannot override the final method from Program

请注意,这还不是一个独立的应用程序。只需从 Eclipse 编辑器运行它。

显然 acm.program 库中有一个 init 方法。如何在不尝试覆盖 acm.program 内置方法的情况下实现自己的初始化方法?我尝试使用 将我的 init 方法设为私有(private)。私有(private)无效初始化 但后来我得到:
- Cannot reduce the visibility of the inherited method from Program
- Cannot override the final method from Program 

这是到目前为止的代码。错误在于 init()。
public class Hangman extends GraphicsProgram {

    //CONSTANTS
private static int NUMBER_OF_INCORRECT_GUESSES = 8;


//Initialize the program
public void init() { //this gives compiler a problem
HangmanCanvas canvas = new HangmanCanvas();
add(canvas);
}



public void run() {

/* 当用户玩刽子手时,计算机首先在
从程序内置的列表中随机选择。然后程序打印出一行破折号——一个
密码中的每个字母——并要求用户猜一个字母。如果用户猜测
单词中的一个字母,该单词与该字母的所有实例一起重新显示
显示在正确的位置,以及在前一回合中正确猜到的任何字母。
如果字母没有出现在单词中,则用户被指控猜错。
用户不断猜测字母,直到 (1) 用户正确猜出所有字母
单词中的字母或 (2) 用户进行了 8 次错误猜测。 */
HangmanLexicon lexicon = new HangmanLexicon();
RandomGenerator rgen = new RandomGenerator();
int wordIndex = rgen.nextInt(0, lexicon.getWordCount()-1);

    while (true) { //allows multi-play
        int play = 0;
        String answer = readLine ("Want to play?");
        if(answer.equals("Y") || answer.equals("y") || answer.equals("yes") || answer.equals("Yes")) {play = 1;}
        if(play == 0) {break;}
        //Initialize some stuff
        //get new random word
        secretWord = lexicon.getWord(rgen.nextInt(0,wordIndex));
        println("Welcome to Hangman.");
        secretWord = secretWord.toUpperCase(); // convert secret word to upper case
        int length = secretWord.length();
        //reset game variables
        String guess = "";
        //clear the canvas
        canvas.reset();
        //reset the wrong answer count
        wrong = 0;

//建立空白状态字
        currentWord = ""; //reset the word for multi-play

//在状态字中建立与 secret 字一样长的破折号。
        for (int i = 0; i < length; i++) {
            currentWord += "-";
        }
        println("The word looks like this  " + currentWord);

        while (wrong<NUMBER_OF_INCORRECT_GUESSES && !currentWord.equals(secretWord)) {
            guess = ".";
            char g = guess.charAt(0);
            while (!Character.isLetter(g)){ //if input is not a character, keep asking
                guess = readLine("Guess a letter: ");
                guess = guess.toUpperCase();
                g = guess.charAt(0);
                if (!Character.isLetter(g)){println("Your guess is not a single letter. Guess again: ");}
            }
            if(secretWord.indexOf(guess) < 0) {/*if guess is not in the secret word, increment wrong answer count and print message
             to that effect. */
                wrong++;
                println("Threre are no " + guess + "\'s in the word.");
                println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong)  + " guesses left.");
            }
            else {
                println("That guess is correct.");
                currentWord = wordBuild(guess);
                if (currentWord.equals(secretWord)) { //if win print win but don't bother with the update to display
                    println("You win! You guessed the word: " + secretWord);}

                    else {  println("The word now looks like this  " + currentWord); //print the updated dash word
                    println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong)  + " guesses left.");
                    }           
            }
        }

    if(!currentWord.equals(secretWord)) {println("You lose.");}   //out of guesses and  word is not good.
    }
}

//构建和/或更新破折号 ------ 显示
    public String wordBuild(String guess) {
        String dashWord = "";
        for (int i = 0; i < secretWord.length(); i++) {
                if(secretWord.charAt(i) == guess.charAt(0)) { 
                dashWord = dashWord + guess;
                }
                    else {dashWord = dashWord + currentWord.charAt(i);
                }
        }
            return dashWord;

    }



//INSTANCE VARS

int wrong = 0;  
String currentWord = "";
String secretWord = "";     
private HangmanCanvas canvas;

} //end of class

最佳答案

我想你正在服用Stanford CS106A course这导致了 init 最终问题。问题是 Karel.jar 图书馆你必须在前几节课中导入。我想该库将 init() 方法作为 final方法,这是根本原因。所以简单的解决方案是从引用库中删除 Karel.jar:

  • 在包资源管理器中选择 引用库 .
  • 右击 Karel.jar .
  • 选择构建路径 从上下文菜单中。
  • 选择从构建路径中删除 .

  • 但是,如果您需要再次使用 Eclipse 进行 Karel 编程,您必须通过遵循类似的类(class)但选择导入库来再次从作业 1 包中导入引用库。
    为确保您继续使用 acm 方法,请确保通过从 jtf.acm.org 下载 acm.jar 来导入它。 .要添加您使用的库 Eclipse Documentation或者只是谷歌它。

    https://stackoverflow.com/questions/9331698/

    相关文章:

    java - Apache Wink请求处理程序

    c++ - 算法::binary_search call中的预期主要表达式

    c# - 如何在ce中创建空数据库并用datagridview显示

    java - 错误包org.python.util不存在,使用ant进行编译

    ios - 由于未捕获的异常 'NSInvalidArgumentException' 错误而终止应

    android - 在Eclipse上调试Android应用时出错

    compiler-errors - Play Framework [2.0]-更改模型破坏了我的见解

    c++ - 如何在 Windows XP 上正确安装 ffmpeg?

    android - 无法在 android opencv 2.3 中导入 ml.h

    triggers - 触发编译错误(插入)