我的任务是为抽象类MashUpPlayer
创建一个子类。 MashUpPlayer有一个构造函数。子类的构造函数必须没有参数。客户端代码将无法编译。我不确定如何在不使用给定参数实现构造函数的情况下编译代码。
我的代码:
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;
}
}
答案 0 :(得分:0)
Wizard() {
super("Some string", Color.Black); // default wizard values
}
答案 1 :(得分:0)
试试这个。
import java.awt.*;
public class Wizard extends MashUpPlayer {
static final String s = "^\u221e^";
static final Color c = Color.GRAY;
Wizard() {
super(s, c);
}
public void setColor(Color c) {
super.setColor(c);
}
public void setDisplay(String s) {
super.setDisplay(s);
}
public Color getColor() {
return super.getColor();
}
public String toString() {
return super.toString();
}
...
}
答案 2 :(得分:0)
你的类继承有点时髦。如果要设置某些特定于子的行为,则应实现抽象方法并覆盖其他方法。从您发布的代码中,您无法直接访问私有变量,而应通过您未执行的公共getter和setter访问它们。您甚至无法实例化您尝试实例化的方式。你的&#39; MashUpPlayer&#39;中无法访问你的s。你应该做这样的事情
MashUpPlayer player = new MashUpPlayer();
player.setColor(Color.GRAY);