Abstrakte Klasse clonen



  • Hallo ich habe eine abstrakte Klasse und möchte sie clonen :

    import java.awt.Point;
    import java.util.HashMap;
    import java.util.Iterator;
    
    public abstract class PlayingField implements Constants, Cloneable{
    
    	private HashMap<Point, PlayerColor> playingField = null;
    	private int fieldToWin 	= 0;
    	private int fieldRow	= 0;
    	private int fieldColumn = 0;
    	private PlayerColor	playingFieldWinner = PlayerColor.NoColor;
    	private PlayingFieldState playingFieldState = PlayingFieldState.NotFinished;
    
    	public PlayingField(int fieldToWin, int fieldRow, int fieldColumn){
    		this.fieldToWin = fieldToWin;
    		this.fieldRow	= fieldRow;
    		this.fieldColumn= fieldColumn;
    		initPlayingField();
    	}
    
    	protected abstract boolean setFieldValue(Point field, PlayerColor value);
    
    	protected abstract HashMap<Point, PlayerColor> getFreeFields();
    
    	protected PlayerColor getFieldValue(Point field){
    		return playingField.get(field);
    	}
    
    	protected PlayerColor getPlayingFieldWinner(){
    		return playingFieldWinner;
    	}
    
    	protected PlayingFieldState getPlayingFieldState(){
    		return playingFieldState;
    	}
    
    	protected void updatePlayingField(Point field){		
    		this.playingFieldWinner 	= PlayerColor.NoColor;
    		this.playingFieldState 		= PlayingFieldState.Finished;
    		FieldColorList tempList[] 	= new FieldColorList[4];
    
    		tempList[0] = createFieldColorListRow(field);
    		tempList[1] = createFieldColorListColumn(field);
    		tempList[2] = createFieldColorListDiagonalLeftDown(field);
    		tempList[3] = createFieldColorListDiagonalRightDown(field);
    
    		for (int i = 0; i < 4; i++){
    			if (tempList[i].colorListWinner != PlayerColor.NoColor){
    				this.playingFieldState = PlayingFieldState.Finished;
    				this.playingFieldWinner = tempList[i].colorListWinner;
    				return;
    			}
    			if (tempList[i].colorListState == PlayingFieldState.NotFinished){
    				this.playingFieldState		= PlayingFieldState.NotFinished;
    			}		
    		}	
    	}
    
    	public Object clone(){
    		PlayingField returnValue = new PlayingField(this.fieldToWin, this.fieldRow, this.fieldColumn);
    		//PlayingField returnValue = new PlayingField(this.fieldToWin, this.fieldRow, this.fieldColumn);
    		returnValue.playingFieldWinner = PlayerColor.NoColor;
    		returnValue.playingFieldState = PlayingFieldState.NotFinished;
    
    		Iterator<Point> it = playingField.keySet().iterator();
    		while(it.hasNext()){
    			Point nextField = it.next();
    			returnValue.playingField.put(nextField, this.playingField.get(nextField));
    		}
    
    		return returnValue;		
    
    	}
    
    	protected void initPlayingField(){
    		for (int x = 0; x < fieldRow; x++){
    			for (int y = 0; y < fieldColumn; y++){
    				playingField.put(new Point(x,y), PlayerColor.NoColor);
    			}
    		}		
    	}
    
    	protected FieldColorList createFieldColorListRow(Point field){
    		FieldColorList returnValue = new FieldColorList(fieldToWin);		
    		for (int x = 0; x < fieldRow; x++){
    			returnValue.addElementToList(playingField.get(new Point(x, field.y)));
    		}		
    		return returnValue;
    	}
    
    	protected FieldColorList createFieldColorListColumn(Point field){
    		FieldColorList returnValue = new FieldColorList(fieldToWin);
    		for (int y = 0; y < fieldColumn; y++){
    			returnValue.addElementToList(playingField.get(new Point(field.x, y)));
    		}
    		return returnValue;
    	}
    
    	protected FieldColorList createFieldColorListDiagonalLeftDown(Point field){
    		FieldColorList returnValue = new FieldColorList(fieldToWin);		
    		Point startField = new Point();
    		int i = 0; 
    
    		startField.x = field.x - field.y;
    		startField.y = 0;		
    		if (startField.x < 0){
    			startField.x = 0;
    			startField.y = field.y - field.x;	
    		}
    
    		while ((startField.x + i < fieldRow) && (startField.y + i< fieldColumn)){
    			returnValue.addElementToList(playingField.get(new Point(startField.x + i, startField.y + i)));
    			i++;
    		}		
    		return returnValue;
    	}
    
    	protected FieldColorList createFieldColorListDiagonalRightDown(Point field){
    		FieldColorList returnValue = new FieldColorList(fieldToWin);
    		Point startField = new Point();
    		int i = 0; 
    
    		startField.x = field.x + field.y;
    		startField.y = 0;	
    		if (startField.x > fieldRow - 1){
    			startField.x = fieldRow - 1;
    			startField.y = field.y - ((fieldRow - 1) - field.x);
    		}
    		while ((startField.x - i >= 0) && (startField.y + i < fieldColumn)){
    			returnValue.addElementToList(playingField.get(new Point(startField.x - i, startField.y +i)));
    			i++;
    		}		
    		return returnValue;
    	}		
    }
    

    Allerdings bekomme ich einen Compiler Fehler. Kann mir jemand sagen wieso und was ich anders machen kann.



  • Sehr viel Code hier, tl;dr.

    Ich kann mich irren, weil ich kein Java-Programmierer bin, aber eine abstrakte Klasse kann man doch gar nicht instanzieren? Darum kannst du die Klon-Methode dort zwar als Schnittstelle bereitstellen, aber implementieren muss du sie in den erbenden Klassen jeweils einzeln.

    😕



  • Danke . Ja du hast recht eine Abtrakte Klasse kann man nicht.


Anmelden zum Antworten