2016-12-02 7 views
0

Meine Aufgabe ist es, eine Unterklasse für die abstrakte Klasse MashUpPlayer zu erstellen. MashUpPlayer hat einen Konstruktor. Die Konstruktoren für die Unterklassen müssen keine Parameter haben. Der Client-Code kann nicht anderweitig kompiliert werden. Ich bin nicht sicher, wie man den Code kompiliert, ohne den Konstruktor mit den gegebenen Parametern zu implementieren.Java - mit Konstruktor aus der abstrakten Klasse

Mein Code:

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; 
    } 
} 

Dies ist die abstrakte Methode:

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; 
    } 
} 
+2

Sie müssen dann mit den Standardwerten aufrufen. –

Antwort

0
Wizard() { 
    super("Some string", Color.Black); // default wizard values 
} 
0

bereits.

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(); 
    } 

    ... 

} 
0

Ihre Klassenvererbung ist ein bisschen funky. Sie sollten die abstrakte Methode implementieren und andere überschreiben, wenn Sie ein kindspezifisches Verhalten festlegen möchten. Aus dem von Ihnen geposteten Code können Sie nicht direkt auf private Variablen zugreifen, sondern auf sie über Ihren öffentlichen Getter und Setter zugreifen, den Sie nicht tun. Sie können nicht einmal die Art instanziieren, die Sie versuchen zu instanziieren. Auf deine Snd und C kannst du in deinem 'MashUpPlayer' nicht zugreifen. Sie sollten so etwas tun

MashUpPlayer player = new MashUpPlayer(); 
player.setColor(Color.GRAY); 
Verwandte Themen