老师给的代码,不过好像有问题。
第一个类:AbstractGuessGame
package zuoye64; public abstract class AbstractGuessGame { private int number; public void setNumber(int number){ this .number = number; } public void start(){ showMessage("欢迎"); int guess = 0; do{ guess = getUserInput(); if(guess>number){ showMessage("输入的数字较大"); }else{ showMessage("猜中了"); } }while(guess!=number); } protected abstract void showMessage(String message); protected abstract int getUserInput(); }
第二个类:TextModeGame
package zuoye64; import java.util.Scanner; public class TextModeGame { private Scanner scanner; public TextModeGame(){ scanner = new Scanner(); } protected void showMessage(){ for(int i = 0; i第三个类:GuessGameDemo
package zuoye64; public class GuessGameDemo { public static void main(String [] args){ AbstractGuessGame guessgame = new TextModeGame(); guessgame.setNumber(50); guessgame.start(); } }TextModeGame类中:
messege没定义。
同时一个Scanner类型变量。这个了解到的是文件读取以及键盘读取。创建变量时好像要给个参数。
GuessGameDemo类中:
AbstractGuessGame guessgame = new TextModeGame();
按c++来说子类在右边但是没有继承。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)