如何解决在Java中是否可以在不使用关键字“ implements”的情况下实现接口及其方法?
我有功课要做(实际上是我的女朋友:-D),我可以做什么和不能做什么都有一些限制。 在我的NetBeans项目文件夹中,有两个名为“ interfaces”和“ homework”的文件夹。 文件夹“ interfaces”包含不允许编辑的接口和类,因为这样告诉我们xD。 我只能在“作业”文件夹中编辑源文本。
通常,我知道使用关键字“ implements”在Java中实现接口。但是我不知道如何在“家庭作业”文件夹中使用它,因为“接口”文件夹已经包含实现接口方法和逻辑的类,但是我不允许对其进行编辑。
这是任务:
“我们在” interfaces“包中为您提供了各种接口,您必须对其进行实现。只允许您在” homework“包中创建自己的实现。您不得修改”接口”包!
必须执行的方法“ TextAdventure”接口允许您创建文本冒险游戏。在“作业”包中,您将找到一个使用TextAdventure接口初始化各种游戏的Main类。 成功实现所需接口后,您可以玩该游戏。游戏场景旨在帮助您广泛地测试代码。 Interface Player的所有方法均可用于与游戏进行交互。还可以看看GameStarter类。这样,便实现了与玩家的互动。
TextAdventure界面提供了用于创建新游戏的各种方法。对于每种方法,请考虑可能会失败的情况。在这种情况下,抛出TextAdventureException。也可以在“接口”包中使用它。一旦建立了所需的初始状态,便可以使用“ startGame()”启动游戏。”
接口:
package interfaces;
import homework.Factory;
/**
* Class with text adventure scenarios to play.
*/
public class Adventures {
/**
* Initialize the fireman-game.
*
* @return the fireman-game
* @throws TextAdventureException
*/
public static interfaces.TextAdventure getFiremanGame() throws TextAdventureException {
interfaces.TextAdventure textAdventure = Factory.getGame("Sample1",2,2);
textAdventure.addSceneryType("BurningTree","A blazing fire on an oak tree.");
textAdventure.addSceneryType("HalfBurningTree","A little fire on an oak tree.");
textAdventure.addItemType("Bucket","An empty bucket.");
textAdventure.addItemType("Water","A bucket full of water.");
textAdventure.addItemType("Head","Kopf");
textAdventure.addItemType("Headweh","Jetzt brummt de kopf");
textAdventure.addSceneryType("Lake","A beautiful lake.");
textAdventure.addSceneryType("CharredOak","A poor,charred oak tree.");
textAdventure.addTransformation("Bucket","Lake","Water","Now you got a bucket full of water.");
textAdventure.addTransformation("Water","BurningTree","HalfBurningTree","Bucket","The water quenches the fire. But not completely.");
textAdventure.addTransformation("Water","CharredOak","The water quenches the fire.");
textAdventure.addComposition("Bucket","Head","Headweh","Kopfwehh");
textAdventure.placeItem("Lake",0);
textAdventure.placeItem("Bucket",1,1);
textAdventure.placeItem("Head",1);
textAdventure.placeItem("BurningTree",0);
return textAdventure;
}
/**
* Initialize the lumberjack-game.
*
* @return The lumberjack-game
* @throws TextAdventureException
*/
public static interfaces.TextAdventure getLumberjackGame() throws TextAdventureException {
interfaces.TextAdventure textAdventure = Factory.getGame("Sample2",1);
textAdventure.addSceneryType("Tree","A lush green tree.");
textAdventure.addItemType("Wood","Some pieces of wood");
textAdventure.addSceneryType("Roots","The sad roots of a former tree.");
textAdventure.addDecomposition("Tree","Wood","Roots","You fell the tree and get some wood.");
textAdventure.placeItem("Tree",0);
return textAdventure;
}
/**
* Initialize the pokemon-game.
*
* @return the pokemon-game
* @throws TextAdventureException
*/
public static interfaces.TextAdventure getPokemonGame() throws TextAdventureException {
TextAdventure textAdventure = Factory.getGame("Pokemon",50,50);
textAdventure.addItemType("Coin","One coin");
textAdventure.addItemType("Money","Many coins");
textAdventure.addItemType("Bisasam","Look,a wild Bisasam.");
textAdventure.addItemType("Glumanda",a wild Glumanda.");
textAdventure.addItemType("Schiggy",a wild Schiggy.");
textAdventure.addItemType("Bisaknosp","Bisaknosp eats plants.");
textAdventure.addItemType("Glutexo","Glutexo burns.");
textAdventure.addItemType("Schillok","Schillok swimming around.");
textAdventure.addItemType("Bisaflor","Bisaflor eats plants.");
textAdventure.addItemType("Glurak","Glurak burns.");
textAdventure.addItemType("Turtok","Turtok swimming around.");
textAdventure.addItemType("Taubsi","Taubsi flying around.");
textAdventure.addItemType("Tauboga","Tauboga flying around.");
textAdventure.addItemType("Tauboss","Tauboss flying around.");
textAdventure.addItemType("Mewtu","Mewtu sees dead people.");
textAdventure.addItemType("Developmentstone","The Developmentstone brings your Pokemon to the next Level.");
textAdventure.addItemType("Attackstone","The Attackstone is in combination with your Pokemon to attack other Pokemons.");
textAdventure.addItemType("Pokeball","Pokeball is there to catch other Pokemons.");
textAdventure.addSceneryType("Wildtaubsi",a wild Taubsi.");
textAdventure.addSceneryType("Deadtaubsi",a dead Taubsi.");
textAdventure.addSceneryType("Silvertreasure",a Treasure.");
textAdventure.addSceneryType("Goldtreasure",a Treasure.");
textAdventure.placeItem("Coin",0);
textAdventure.placeItem("Bisasam",0);
textAdventure.placeItem("Glumanda",0);
textAdventure.placeItem("Schiggy",0);
textAdventure.placeItem("Developmentstone",4);
textAdventure.placeItem("Developmentstone",25,31);
textAdventure.placeItem("Developmentstone",49,11);
textAdventure.placeItem("Developmentstone",22,22);
textAdventure.placeItem("Developmentstone",11,44);
textAdventure.placeItem("Developmentstone",23,43);
textAdventure.placeItem("Developmentstone",39,1);
textAdventure.placeItem("Developmentstone",21);
textAdventure.placeItem("Developmentstone",45,30);
textAdventure.placeItem("Developmentstone",10,12);
textAdventure.placeItem("Developmentstone",49);
textAdventure.placeItem("Developmentstone",17,15);
textAdventure.placeItem("Attackstone",1);
textAdventure.placeItem("Attackstone",10);
textAdventure.placeItem("Attackstone",20,20);
textAdventure.placeItem("Attackstone",30,30);
textAdventure.placeItem("Attackstone",4,5);
textAdventure.placeItem("Attackstone",2);
textAdventure.placeItem("Attackstone",44);
textAdventure.placeItem("Attackstone",35);
textAdventure.placeItem("Pokeball",1);
textAdventure.placeItem("Pokeball",3,22);
textAdventure.placeItem("Pokeball",21);
textAdventure.placeItem("Pokeball",33,13);
textAdventure.placeItem("Pokeball",21,19,41);
textAdventure.placeItem("Pokeball",12,32);
textAdventure.placeItem("Wildtaubsi",2);
textAdventure.placeItem("Wildtaubsi",24);
textAdventure.placeItem("Wildtaubsi",7);
textAdventure.placeItem("Wildtaubsi",9);
textAdventure.placeItem("Wildtaubsi",32,45);
textAdventure.placeItem("Wildtaubsi",31,29);
textAdventure.placeItem("Wildtaubsi",31);
textAdventure.placeItem("Wildtaubsi",18,26,42);
textAdventure.placeItem("Wildtaubsi",42,26);
textAdventure.placeItem("Wildtaubsi",49);
textAdventure.placeItem("Wildtaubsi",22);
textAdventure.placeItem("Wildtaubsi",36,39);
textAdventure.placeItem("Wildtaubsi",24);
textAdventure.placeItem("Silvertreasure",3);
textAdventure.placeItem("Silvertreasure",33);
textAdventure.placeItem("Silvertreasure",14,13);
textAdventure.placeItem("Silvertreasure",47,11);
textAdventure.placeItem("Silvertreasure",34);
textAdventure.placeItem("Silvertreasure",43,21);
textAdventure.placeItem("Silvertreasure",49);
textAdventure.placeItem("Silvertreasure",41,40);
textAdventure.placeItem("Silvertreasure",28);
textAdventure.placeItem("Silvertreasure",32);
textAdventure.placeItem("Silvertreasure",23);
textAdventure.placeItem("Goldtreasure",34);
textAdventure.addComposition("Coin","Coin","Money","You made money");
textAdventure.addComposition("Bisasam","Developmentstone","Bisaknosp","Look your Bisasam has developed to Bisaknosp.");
textAdventure.addComposition("Glumanda","Glutexo","Look your Glumanda has developed to Glutexo.");
textAdventure.addComposition("Schiggy","Schillok","Look your Schiggy has developed to Schillok.");
textAdventure.addComposition("Taubsi","Tauboga","Look your Taubsi has developed to Tauboga");
textAdventure.addComposition("Bisaknosp","Bisaflor","Look your Bisaknosp has developed to Bisaflor");
textAdventure.addComposition("Glutexo","Glurak","Look your Glutexo has developed to Glurak");
textAdventure.addComposition("Schillok","Turtok","Look your Schillok has developed to Turtok");
textAdventure.addComposition("Tauboga","Tauboss","Look your Tauboga has developed to Tauboss");
textAdventure.addComposition("Wildtaubsi","Attackstone","Deadtaubsi","Your Pokemon attacks the wild Taubsi. Taubsi is dead.");
textAdventure.addComposition("Wildtaubsi","Pokeball","Taubsi","You have catch Taubsi. Congratulations,Taubsi is your new Pokemon.");
textAdventure.addDecomposition("Silvertreasure","You open the treasure and you find an Attack- and Developmentstone.");
textAdventure.addDecomposition("Goldtreasure","Mewtu","See,you find the unique Mewtu.");
textAdventure.addTransformation("Taubsi",your Taubsi transform the Attackstone to a Developmentstone.");
textAdventure.addTransformation("Tauboga",your Tauboga transform the Attackstone to a Developmentstone.");
textAdventure.addTransformation("Tauboss",your Tauboss transform the Attackstone to a Developmentstone.");
textAdventure.addTransformation("Mewtu",your Mewtu transform the Developmentstone to a Attackstone.");
textAdventure.addTransformation("Bisasam","Bisasam",your Bisasam transform the Attackstone to a Pokeball.");
textAdventure.addTransformation("Bisaknosp",your Bisaknosp transform the Attackstone to a Pokeball.");
textAdventure.addTransformation("Bisaflor",your Bisaflor transform the Attackstone to a Pokeball.");
textAdventure.addTransformation("Glumanda","Glumanda",your Glumanda transform the Pokeball to a Developmentstone.");
textAdventure.addTransformation("Glutexo",your Glutexo transform the Pokeball to a Developmentstone.");
textAdventure.addTransformation("Glurak",your Glurak transform the Pokeball to a Developmentstone.");
textAdventure.addTransformation("Schiggy","Schiggy",your Schiggy transform the Pokeball to a Attackstone.");
textAdventure.addTransformation("Schillok",your Schillok transform the Pokeball to a Attackstone.");
textAdventure.addTransformation("Turtok",your Turtok transform the Pokeball to a Attackstone.");
return textAdventure;
}
}
package interfaces;
import homework.Factory;
/**
* Class integrates the implementation of the student package and allows to play the text adventure scenarios.
*/
public class GameStarter {
private static final String PROMPT = "play>";
private static Player player;
private Terminal terminal;
private TextAdventure[] games;
/**
* Constructor to create a GameStarter instance.
* @param games array with text adventures games to play
*/
public GameStarter(TextAdventure[] games) {
terminal = Factory.getTerminal();
this.games = games;
}
/**
* Starts one of the given games.
*/
public void startGame() {
String[] input;
String prompt = "Play (";
for (TextAdventure game : this.games) {
prompt += game.getName();
prompt += "|";
}
prompt = prompt.substring(0,prompt.length() - 1) + ")";
do {
terminal.promptInput(prompt);
input = terminal.readInput();
} while (input[0].equals("") || input[0].equals(" "));
String gameName = input[0];
for (TextAdventure game : this.games) {
if (game.getName().equals(gameName)) {
startGame(game,0);
return;
}
}
System.out.println("Unknown game scenario");
System.exit(1);
}
/**
* Starts a given game and runs a simple input-loop that accepts game-commands.
* @param textAdventure to play
* @param x coordinate of the player
* @param y coordinate of the player
*/
public void startGame(TextAdventure textAdventure,int x,int y) {
try {
player = textAdventure.startGame(x,y);
} catch (TextAdventureException e) {
System.out.println(e.getMessage());
System.exit(1);
}
boolean run = true;
while (run) {
try {
terminal.promptInput(PROMPT);
String[] input = terminal.readInput();
run = processInput(input);
} catch (TextAdventureException e) {
System.out.println(e.getMessage());
}
}
}
/**
* Process the given command-line,translate it into player-actions if possible.
* @param input the splitted user input
* @return false if the user wants to quit,true otherwise
* @throws TextAdventureException If an invalid command is entered
*/
private static boolean processInput(String[] input) throws TextAdventureException {
switch (input[0]) {
case "go": {
if (input.length < 2) {
System.out.println("Sorry,please specify a direction.");
} else {
System.out.println(player.go(input[1]));
}
return true;
}
case "look": {
String[] list = player.look();
for (String line : list) {
System.out.println(line);
}
return true;
}
case "inventory": {
String[] list = player.inventory();
for (String line : list) {
System.out.println(line);
}
return true;
}
case "take": {
if (input.length < 2) {
System.out.println("Sorry,please specify an object.");
} else {
System.out.println(player.take(input[1]));
}
return true;
}
case "drop": {
if (input.length < 2) {
System.out.println("Sorry,please specify an object.");
} else {
System.out.println(player.drop(input[1]));
}
return true;
}
case "convert": {
if (input.length < 3) {
System.out.println("Sorry,please specify the objects to compose.");
} else {
System.out.println(player.convert(input[1],input[2]));
}
return true;
}
case "decompose": {
if (input.length < 2) {
System.out.println("Sorry,please specify an object.");
} else {
System.out.println(player.decompose(input[1]));
}
return true;
}
case "help": {
System.out.println("Valid Commands are: go,look,inventory,take,drop,compose,decompose");
return true;
}
case "exit": {
System.out.println("Bye!");
return false;
}
default: {
throw new TextAdventureException("Unknown Command: " + input[0]);
}
}
}
}
package interfaces;
/**
* Models the interface of a text adventure player.
*/
public interface Player {
/**
* Move the player's token into direction.
* @param direction One of eight valid directions "N","NE","E","SE","S","SW","W","NW"
* @return A ui-message for the user
*/
String go(String direction);
/**
* Returns an array of objects on the current field.
* @return An array of object-type and descriptions for the ui
*/
String[] look();
/**
* Returns an array of objects in the inventory.
* @return An array of object-type and descriptions for the ui
*/
String[] inventory();
/**
* Removes an object of the given type from the current field and adds it to the inventory.
* @param item An object-type
* @return A ui-message for the user
*/
String take(String item);
/**
* Removes an object of the given type from the inventory and adds it to the current field.
* @param item An object-type
* @return A ui-message for the user
*/
String drop(String item);
/**
* Applies a transformation- or composition-rule to item1 and item2.
* If possible,removes item1 and item2 from the current field or inventory
* and adds the mutation-result to the current field or inventory,* depending on the object's base-type (scenery or item).
* @param item1 An Object-Type
* @param item2 An Object-Type
* @return A ui-message for the user
*/
String convert(String item1,String item2);
/**
* Applies a decomposition-rule to item.
* If possible,removes item from the current field or inventory
* and adds the mutation-result to the current field or inventory,* depending on the object's base-type (scenery or item).
* @param item An Object-Type
* @return A ui-message for the user
*/
String decompose(String item);
}
package interfaces;
/**
* Models the interface of terminal to interact with a player.
*/
public interface Terminal {
/**
* Prompts the user to enter a string as input. The string is printed to the standard output (terminal).
*
* @param input the message to display the player
*/
public void promptInput(String input);
/**
* Reads the user input and returns splitted at spaces as string array.
* @return splitted user input string
*/
public String[] readInput();
}
package interfaces;
/**
* Models the interface of a text adventure game-instance.
*/
public interface TextAdventure {
/**
* Declares a new portable object-type with given id and description.
* @param id The id object-type,used in the ui to search for objects
* @param description The description of the object-type
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void addItemType(String id,String description) throws TextAdventureException;
/**
* Declares a new non-portable object-type with given id and description.
* @param id The id object-type,used in the ui to search for objects
* @param description The description of the object-type
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void addSceneryType(String id,String description) throws TextAdventureException;
/**
* Adds a new object of the given type to the field at the specified position.
* @param type The id of an object-type
* @param x A field coordinate on the board
* @param y A field coordinate on the board
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void placeItem(String type,int y) throws TextAdventureException;
/**
* Adds a new composition-rule to the set of mutation-rules.
* @param in1 Object-type id of the input-object
* @param in2 Object-type id of the input-object
* @param out Object-type id of the output-object
* @param description Mutation-description for the ui
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void addComposition(String in1,String in2,String out,String description) throws TextAdventureException;
/**
* Adds a new decomposition-rule to the set of mutation-rules.
* @param in Object-type id of the input-object
* @param out1 Object-type id of the output-object
* @param out2 Object-type id of the output-object
* @param description Mutation-description for the ui
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void addDecomposition(String in,String out1,String out2,String description) throws TextAdventureException;
/**
* Adds a new transformation-rule to the set of mutation-rules.
* @param in1 in1 Object-type id of the input-object
* @param in2 in1 Object-type id of the input-object
* @param out1 Object-type id of the output-object
* @param out2 Object-type id of the output-object
* @param description Mutation-description for the ui
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
void addTransformation(String in1,String description)
throws TextAdventureException;
/**
* Initializes a new game instance and returns a player-instance for game-control.
* The player's initial position is at the given coordinates
* @param x A field coordinate on the board
* @param y A field coordinate on the board
* @return A player-instance for game-control
* @throws TextAdventureException Think about the cases in which an exception is useful and implement it.
*/
Player startGame(int x,int y) throws TextAdventureException;
/**
* Returns the name of the game instance.
* @return the name of the game instance
*/
String getName();
}
package interfaces;
/**
* General Exception-type of the text adventure framework.
*/
public class TextAdventureException extends Exception {
/**
* Exception Prefix.
*/
public static final String ERROR = "Error! ";
/**
* Creates a new TextAdventureException with the given message.
* @param message The error-message
*/
public TextAdventureException(String message) {
super(ERROR + message);
}
}
package homework;
/**
* Factory class with functions to generate a game and terminal.
*/
public final class Factory {
private Factory() { }
public static interfaces.TextAdventure getGame(String name,int boardWidth,int boardHeight)
throws interfaces.TextAdventureException {
//placeholder
return null;
}
/**
* Creates a new terminal-instance to read user input and prompt messages.
* @return a terminal instance
*/
public static interfaces.Terminal getTerminal() {
//placeholder
return null;
}
}
一揽子作业; 公共课程主要{
public static void main(String[] args) {
try {
// Add additional games to the Array
TextAdventure[] games = {
Adventures.getFiremanGame(),Adventures.getLumberjackGame(),Adventures.getPokemonGame()
};
GameStarter starter = new GameStarter(games);
starter.startGame();
} catch (TextAdventureException e) {
e.printStackTrace();
}
}
}
解决方法
您可以实现接口X声明的所有方法,而无需编写“实现X”。但这确实是荒谬的。您希望Java编译器理解您的新类实现了X。并且您需要在类定义的签名上使用该关键字。
可以考虑扩展一个已经实现接口的类,那么您不必重复关键字“ implements X”。但这实际上只是在源代码中不使用该关键字。
对于您而言,关键部分是要了解到目前为止,程序包接口中的所有接口还没有实现。您的起点:仅写下接收到的输入中存在的类和接口的名称列表。然后查看哪个接口有实现,哪些没有实现!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。