[英]Java - using constructor from abstract class
我的任务是为抽象类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;
}
}
Wizard() {
super("Some string", Color.Black); // default wizard values
}
尝试这个。
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();
}
...
}
您的类继承有点时髦。 如果要设置某些特定于孩子的行为,则应实现abstract方法并覆盖其他方法。 从发布的代码中,您不能直接访问私有变量,而应该通过您未使用的公共getter和setter来访问它们。 您甚至无法实例化尝试实例化的方式。 您的snd c无法在“ MashUpPlayer”中访问。 你应该做这样的事情
MashUpPlayer player = new MashUpPlayer();
player.setColor(Color.GRAY);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.