我的任務是爲抽象類MashUpPlayer
創建一個子類。 MashUpPlayer有一個構造函數。子類的構造函數必須沒有參數。客戶端代碼將無法進行其他編譯。我不確定如何在不使用給定參數實現構造函數的情況下編譯代碼。Java - 使用抽象類的構造函數
我的代碼:
public class Wizard extends MashUpPlayer {
//this shouldn't have any params ??
Wizard (String s, Color c){
super(s,c);
}
/**
* method to set the color of Wizard MashUpPlayer to Gray
* @param c is the color
*/
public void setColor(Color c){
c = Color.GRAY;
}
/**
*method to change the String state to "^\u221e^"
*@param s is the string display
**/
public void setDisplay (String s){
s= "^\u221e^";
}
/**overrides getColor method from MashUpPlayer
* @return Color.GRAY
*/
public Color getColor(){
return Color.GRAY;
}
/**overrides toString method from MashUpPlayer
* @return "^\u221e^"
*/
public String toString(){
return "^\u221e^";
}
/**
* this method is the actions of the Wizard
* It will fight an Other in front of it
* @return Action.FIGHT
* It will turn right if the neighbor in front is a Wizard or a Wall
* @return Action.RIGHT
* It will travel around it's domain otherwise
* @return move 5 spaces, turn right, repeat
*/
public Action getMove(MashUpPlayerInfo info){
//checks neighbor and fights if other
if(info.getFront() ==Neighbor.OTHER){
return Action.FIGHT;
}
//turns right at wall or neighbor
if((info.getFront()==Neighbor.WALL)||(info.getFront()==Neighbor.SAME)){
return Action.RIGHT;
}
//moves 5 spaces
if (info.getFront()==Neighbor.EMPTY){
for (int i=1;i<=5;i++){
return Action.MOVE;
}
}
//turns right
return Action.RIGHT;
}
}
這是抽象方法:
import java.awt.*;
/**
* This is the superclass of all of the MashUpPlayer classes.
* String representation. Some methods must be overriden.
* The class provides several kinds of constants:<p>
* type Neighbor : WALL, EMPTY, SAME, OTHER<br>
* type Action : HOP, LEFT, RIGHT, FIGHT<br>
* type Direction : NORTH, SOUTH, EAST, WEST<br><br> </p>
*
* Based on work by Stuart Reges and Marty Stepp
*/
public abstract class MashUpPlayer {
private String myDisplay;
private Color myColor;
/**
* Initializes this MashUpPlayer's state
* @param s this MashUpPlayer's initial String representation
* @param c this MashUpPlayer's starting color
*/
public MashUpPlayer(String s, Color c){
myDisplay = s;
myColor = c;
}
/**
* This method answers this MashUpPlayer's color
* @return the current color
*/
public Color getColor(){
return myColor;
}
/**
* This method answers this MashUpPlayer's String representation
* @return the current string
*/
public String toString(){
return myDisplay;
}
/**
* This method allows subclasses only to change this MashUpPlayer's color
* @param c the new color
*/
protected void setColor(Color c){
myColor = c;
}
/**
* This method allows subclasses only to change this MashUpPlayer's String display
* @param s the new display
*/
protected void setDisplay(String s){
myDisplay = s;
}
/**
* This method answers this MashUpPlayer's Action
* MUST BE OVERRIDDEN IN SUBCLASSES
*
* @param info information about this MashUpPlayer in the simulation
* @return the current Action
*/
public abstract Action getMove(MashUpPlayerInfo info);
/**
* <div>
* WALL: against the wall of the simulation world<br>
* EMPTY: the neighboring spot is empty<br>
* SAME: an MashUpPlayer of the same species<br>
* OTHER: an MashUpPlayer of another species<br></div>
*/
public static enum Neighbor {
WALL, EMPTY, SAME, OTHER
};
/**
* <div>
* MOVE: move one space in the current direction<br>
* LEFT: turn left by rotating 90 degrees counter-clockwise<br>
* RIGHT: turn right by rotating 90 degrees clockwise<br>
* FIGHT: fight the MashUpPlayer in front of you</div>
*/
public static enum Action {
MOVE, LEFT, RIGHT, FIGHT
};
/**
* <div>
* NORTH<br>
* SOUTH<br>
* EAST<br>}
* WEST</div>
*/
public static enum Direction {
NORTH, SOUTH, EAST, WEST
};
// This prevents any MashUpPlayer from trying to redefine the definition of
// object equality, which is important for the simulator to work properly.
public final boolean equals(Object other) {
return this == other;
}
}
你將不得不用默認值調用它。 –