簡體   English   中英

Java-使用抽象類中的構造函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM