如何使一个类在java的构造函数中接受不同类型的子类(另一个父类的子类)
我正在为此奋斗很多,所以请不要对我太苛刻,因为我对这一切还是新手如何使一个类在java的构造函数中接受不同类型的子类(另一个父类的子类),java,inheritance,constructor,Java,Inheritance,Constructor,我正在为此奋斗很多,所以请不要对我太苛刻,因为我对这一切还是新手 我有一个名为Player的类,其中一个参数是位置。然而,我现在遇到了一个我不知道如何解决的问题。我已经创建了多个子类Location,比如HotelRoom和HotelKitchen,我需要能够从创建的Player对象访问子类中的方法 现在我不能仅仅改变玩家的构造器来接受HotelRoom,因为角色的位置可能会改变为HotelKitchen 球员级别: public class Player { private int h
我有一个名为Player的类,其中一个参数是位置。然而,我现在遇到了一个我不知道如何解决的问题。我已经创建了多个子类Location,比如HotelRoom和HotelKitchen,我需要能够从创建的Player对象访问子类中的方法 现在我不能仅仅改变玩家的构造器来接受HotelRoom,因为角色的位置可能会改变为HotelKitchen 球员级别:
public class Player {
private int health;
private int cluesLeft; //1 clue per level
private String name;
private Location currentRoom;
public Player(String name, int health, int cluesLeft, Location currentRoom) {
this.setHealth(health);
this.setCluesLeft(cluesLeft);
this.setName(name);
this.setCurrentRoom(currentRoom);
}
/**
* @return the health
*/
public int getHealth() {
return health;
}
/**
* @param health the health to set
*/
public void setHealth(int health) {
this.health = health;
}
/**
* @return the cluesLeft
*/
public int getCluesLeft() {
return cluesLeft;
}
/**
* @param cluesLeft the cluesLeft to set
*/
public void setCluesLeft(int cluesLeft) {
this.cluesLeft = cluesLeft;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the currentRoom
*/
public Location getCurrentRoom() {
return currentRoom;
}
/**
* @param currentRoom the currentRoom to set
*/
public void setCurrentRoom(Location currentRoom) {
this.currentRoom = currentRoom;
}
}
位置类
import java.util.Scanner;
/**
*
* @author User
*/
public class Location {
static Scanner scan = new Scanner(System.in);
private String locationImage; // string holds the ASCII map
private String locationName;
private String[] itemImages;
public Location(String locationName, String locationImage, String[] itemImages) {
this.setLocationName(locationName);
this.setLocationImage(locationImage);
this.setItemImages(itemImages);
}
/**
* @return the locationImage
*/
public String getLocationImage() {
return locationImage;
}
/**
* @param locationImage the locationImage to set
*/
public void setLocationImage(String locationImage) {
this.locationImage = locationImage;
}
/**
* @return the locationName
*/
public String getLocationName() {
return locationName;
}
/**
* @param locationName the locationName to set
*/
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public void printLocationMap() {
try {
Thread.sleep(1500);
} catch (Exception e) {
System.out.print(e);
}
System.out.println(this.getLocationImage());
}
public String retrieveSpecificItem(int item){
String[] array = this.getItemImages();
for (int x = 0; x < array.length; x++) {
if (item == (x + 1)) {
return array[x];
}
else {
return null;
}
}
return null;
}
/**
* @return the itemImages
*/
public String[] getItemImages() {
return itemImages;
}
/**
* @param itemImages the itemImages to set
*/
public void setItemImages(String[] itemImages) {
this.itemImages = itemImages;
}
下面是我的ApplicationTester类(具有main)中的一个方法,该方法创建一个location对象,然后将其设置为参数
public static Location setLocations(int whichRoom) {
Location locations = null;
String room = ("|----------| [1] |----------|\n"+
"| [CUPBOARD] [PC] |\n"+
"| P |\n"+
"| |\n"+
"| [BED] |\n"+
"| | | |\n"+
"| | X | |\n"+
"| |___| |\n"+
"|-------------------------------|\n");
String kitchen = ("|----------| [1] |----------|\n"+
"| [FRIDGE] [OVEN] |\n"+
"| P |\n"+
"| |\n"+
"| [SERVING TABLE] |\n"+
"| |\n"+
"| [TABLES] |\n"+
"| |\n"+
"|-------------------------------|\n");
if (whichRoom == 1) {
Location hotelRoom = new Location("Hotel Room", room, allItems(1));
locations = hotelRoom;
}
else {
Location hotelKitchen = new Location("Hotel Kitchen", kitchen, allItems(2));
locations = hotelKitchen;
}
return locations;
}
上述方法使用的allitems(数字)方法
public static String[] allItems(int area) {
String pc = "\n" +
" ____________________________________________________\n" +
" / \\\n" +
" | _____________________________________________ |\n" +
" | | ___________________ | |\n" +
" | | | | | |\n" +
" | | DuckDuckGo - |How to remove stain| | |\n" +
" | | |___________________| | |\n" +
" | | | |\n" +
" | | | |\n" +
" | | | |\n" +
" | | Removing stains from any surface | |\n" +
" | | | |\n" +
" | | Getting rid of stains in 3 steps | |\n" +
" | | | |\n" +
" | | Choosing the right stain remover | |\n" +
" | | | |\n" +
" | |_____________________________________________| |\n" +
" | |\n" +
" \\\\_____________________________________________________/\n" +
" \\\\_______________________________________/\n" +
" _______________________________________________\n" +
" _-' .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. --- `-_\n" +
" _-'.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.--. .-.-.`-_\n" +
" _-'.-.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-`__`. .-.-.-.`-_\n" +
" _-'.-.-.-.-. .-----.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-----. .-.-.-.-.`-_\n" +
" _-'.-.-.-.-.-. .---.-. .-----------------------------. .-.---. .---.-.-.-.`-_\n" +
":-----------------------------------------------------------------------------\n" +
"`---._.-----------------------------------------------------------------._.---";
String cupboard = " __________\n" +
" / /|\n" +
" /__________/ |\n" +
" |________ | |\n" +
" /_____ /|| |\n" +
"|\".___.\"| || |\n" +
"|_______|/ | |\n" +
" || .___.\"|| /\n" +
" ||_______|| /\n" +
" |_________|/\n";
String bed = " __.--'|\n" +
" ___.---' |\n" +
" ___.---' |\n" +
" _.--' \\\\\\ |\n" +
" | XX | |\n" +
" | U. ' |\n" +
" J __=__ |\n" +
" L / |\n" +
" L / /|. .| -`-.\n" +
" | | ||____| __.>-._\n" +
" | | || |__.--' `--._\n" +
" J-._ _____.---------' `--.__\n" +
" J `-<_ `-.__\n" +
" L `-. _>-.\n" +
" +. `-._ ___.--' |\n" +
" | `-._ `-._ __.--' |\n" +
" `-._ `-. __.--' _.--'\n" +
" `-._ `-._.-' _.--' |\n" +
" `-. | _.--'\n" +
" `-. | _.--'\n" +
" `-. | _.--'\n" +
" `|'\n" +
" |";
String sink = " ___\n" +
" .' _ '.\n" +
" / /` `\\ \\\n" +
" | | [__]\n" +
" | | {{\n" +
" | | }}\n" +
" _ | | _ {{\n" +
" ___________<_>_| |_<_>}}________\n" +
" .=======^=(___)=^={{====.\n" +
" / .----------------}}---. \\\n" +
" / / {{ \\ \\\n" +
" / / }} \\ \\\n" +
" ( '=========================' )\n" +
" '-----------------------------'";
String couch = ".----.\n" +
" | |\n" +
" |____|\n" +
" _.-------------._()_.-------------._\n" +
" / | | )( | | \\\n" +
" | | | () | | |\n" +
" _\\|_____________| )( |_____________|/_\n" +
".=. / /(__)\\ \\ .=.\n" +
"|U|/_____________/______\\_____________\\|U|\n" +
"| |====================================| |\n" +
"| | | |\n" +
"|_|LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLlc|_|";
String bin = " _.,-----/=\\-----,._\n" +
" (__ ~~~\"\"\"\"\"\"\"~~~ __)\n" +
" | ~~~\"\"\"\"\"\"\"\"\"~~~ |\n" +
" | | ; , , ; | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" |. \\_| | | |_/ .|\n" +
" `-,.__ ~~~ __.,-'\n" +
" ~~~~~";
String[] hotelRoom = {pc, cupboard, bed};
String[] hotelKitchen = {bin, couch, sink};
if (area == 1) {
return hotelRoom;
} else if (area == 2) {
return hotelKitchen;
} else if (area == 3) {
return null; //not set up yet
} else if (area == 4){
return null; //not set up yet
} else {
return null; //not set up yet
}
}
公共静态字符串[]所有项(int区域){
字符串pc=“\n”+
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”+
“/\\\n”+
“|uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”+
“| | | | | | | | | | | | | | | | | | | | | | \n”+
“| | | | | \n”+
“DuckDuckGo-|如何去除污渍| | | | \n”+
“| | | | | | | | | | | | | | | | | | \n”+
“| | | | \n”+
“| | | | \n”+
“| | | | \n”+
“| |清除任何表面的污渍| |\n”+
“| | | | \n”+
“| |分三步去除污渍| |\n”+
“| | | | \n”+
“| |选择正确的去污剂| | \n”+
“| | | | \n”+
“| | | | uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu|\n”+
“| | \n”+
“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\+
“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\+
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”+
“'.-'.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-'-.-.-.-'-.-.-.-'-.-.-'-.-.-'-.-.-'-.-'-.-.-'-'-.-'-.-'-.-'-+
“'-'.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-'-.-.-'-.-.-'-.-.-'-.-'-.-''-.-.-'''-.-.-'-.-'+
“'.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-''.-.-.-'-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-+
“——”————————————————————————————————————————————————————————————————”+
“——”——————————————————————+
“:----------------------------------------------------------------------------------------\n”+
"`---._.-----------------------------------------------------------------._.---";
串柜=“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu+
“//| \n”+
“/\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu/\n”+
“| | | | | | | \n”+
“/__/| | | | \n”+
“|\.”| | | | | | \n”+
“|| | | | | | |/| | \n”+
“| |.| |.\“| |/\n”+
“| | | | | | | |/\n”+
“|uuuuuuuuuuuuuuuu124;/\ n”;
字符串bed=“_-”|\n”+
“__。--”|\n”+
“__。--”|\n”+
“——”\n”+
“|XX | | \n”+
“|U.|\n”+
“J _=_|\n”+
“L/| \n”+
“L/|…|-`-.\n”+
“|| | | | | | | | | | | | | | | | | | | | | 124+
“| | | | | | | | | | | | | | | | | | | |+
“J-.-.-.-'''''`.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-''''''''''''''''''''''''.-.-.-.'\n”+
“J`-.\n”+
“+。`-.\uuuuu.-'.\n”+
“|`-.`-..-.-'.\n”+
“`-.'-.'-.'-.'-.'-.'-.'-.'-.'-.\n”+
“`-.'-.'.'.-.'.-..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-+
“`-.|.-'\n”+
“`-.|.-'\n”+
“`-.|.-'\n”+
“`|”“\n”+
" |";
字符串接收器=“\uuuuuuuuuu\n”+
“。\n”+
“/``\\\\n”+
“| |[uuuu]\n”+
“| |{\n”+
“| |}}\n”+
“124; |{{\ n”+
“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”+
“====^=(\uuuuuu)=^={{{===。\n”+
“/.--------------.\\\n”+
public static String[] allItems(int area) {
String pc = "\n" +
" ____________________________________________________\n" +
" / \\\n" +
" | _____________________________________________ |\n" +
" | | ___________________ | |\n" +
" | | | | | |\n" +
" | | DuckDuckGo - |How to remove stain| | |\n" +
" | | |___________________| | |\n" +
" | | | |\n" +
" | | | |\n" +
" | | | |\n" +
" | | Removing stains from any surface | |\n" +
" | | | |\n" +
" | | Getting rid of stains in 3 steps | |\n" +
" | | | |\n" +
" | | Choosing the right stain remover | |\n" +
" | | | |\n" +
" | |_____________________________________________| |\n" +
" | |\n" +
" \\\\_____________________________________________________/\n" +
" \\\\_______________________________________/\n" +
" _______________________________________________\n" +
" _-' .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. --- `-_\n" +
" _-'.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.--. .-.-.`-_\n" +
" _-'.-.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-`__`. .-.-.-.`-_\n" +
" _-'.-.-.-.-. .-----.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-----. .-.-.-.-.`-_\n" +
" _-'.-.-.-.-.-. .---.-. .-----------------------------. .-.---. .---.-.-.-.`-_\n" +
":-----------------------------------------------------------------------------\n" +
"`---._.-----------------------------------------------------------------._.---";
String cupboard = " __________\n" +
" / /|\n" +
" /__________/ |\n" +
" |________ | |\n" +
" /_____ /|| |\n" +
"|\".___.\"| || |\n" +
"|_______|/ | |\n" +
" || .___.\"|| /\n" +
" ||_______|| /\n" +
" |_________|/\n";
String bed = " __.--'|\n" +
" ___.---' |\n" +
" ___.---' |\n" +
" _.--' \\\\\\ |\n" +
" | XX | |\n" +
" | U. ' |\n" +
" J __=__ |\n" +
" L / |\n" +
" L / /|. .| -`-.\n" +
" | | ||____| __.>-._\n" +
" | | || |__.--' `--._\n" +
" J-._ _____.---------' `--.__\n" +
" J `-<_ `-.__\n" +
" L `-. _>-.\n" +
" +. `-._ ___.--' |\n" +
" | `-._ `-._ __.--' |\n" +
" `-._ `-. __.--' _.--'\n" +
" `-._ `-._.-' _.--' |\n" +
" `-. | _.--'\n" +
" `-. | _.--'\n" +
" `-. | _.--'\n" +
" `|'\n" +
" |";
String sink = " ___\n" +
" .' _ '.\n" +
" / /` `\\ \\\n" +
" | | [__]\n" +
" | | {{\n" +
" | | }}\n" +
" _ | | _ {{\n" +
" ___________<_>_| |_<_>}}________\n" +
" .=======^=(___)=^={{====.\n" +
" / .----------------}}---. \\\n" +
" / / {{ \\ \\\n" +
" / / }} \\ \\\n" +
" ( '=========================' )\n" +
" '-----------------------------'";
String couch = ".----.\n" +
" | |\n" +
" |____|\n" +
" _.-------------._()_.-------------._\n" +
" / | | )( | | \\\n" +
" | | | () | | |\n" +
" _\\|_____________| )( |_____________|/_\n" +
".=. / /(__)\\ \\ .=.\n" +
"|U|/_____________/______\\_____________\\|U|\n" +
"| |====================================| |\n" +
"| | | |\n" +
"|_|LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLlc|_|";
String bin = " _.,-----/=\\-----,._\n" +
" (__ ~~~\"\"\"\"\"\"\"~~~ __)\n" +
" | ~~~\"\"\"\"\"\"\"\"\"~~~ |\n" +
" | | ; , , ; | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" | | | | | | | |\n" +
" |. \\_| | | |_/ .|\n" +
" `-,.__ ~~~ __.,-'\n" +
" ~~~~~";
String[] hotelRoom = {pc, cupboard, bed};
String[] hotelKitchen = {bin, couch, sink};
if (area == 1) {
return hotelRoom;
} else if (area == 2) {
return hotelKitchen;
} else if (area == 3) {
return null; //not set up yet
} else if (area == 4){
return null; //not set up yet
} else {
return null; //not set up yet
}
}