2017-01-19 37 views
-4

我有一個方形類,並且我想創建一個擴展並繼承所有方形類屬性的圓類......如何使圓類能夠做到這一點。將方形類繼承爲圓類

方形類有繪製,顏色,下降和大小屬性。我要如何圓類從廣場類

廣場類

package GamePackage; 

import java.awt.*; 
import java.awt.geom.Rectangle2D; 
import java.util.Random; 

public class Square { 

    public int XLocation; 
    public int Size; 
    public int YLocation = -Size; 
    public int fallSpeed = 1; 
    Random rand = new Random(); 


    int R = rand.nextInt((256-40)+1)+40; 
    int G = rand.nextInt((256-40)+1)+40; 
    int B= rand.nextInt((256-40)+1)+40; 
    Color RandomColour = new Color (R, G, B); 

    public int generateRandomXLocation(){ 
     return XLocation = rand.nextInt(Game.WINDOW_WIDTH - Size); 
    } 

    /* 
    //creates a random value and stores it in squareWidth 
    */ 
    public int generateRandomSquareSize(){ 
     return Size = rand.nextInt((30-17)+1)+17; 
    } 



    public int generateRandomFallSpeed(){ 
     return fallSpeed = rand.ints(3, 3, 8).findFirst().getAsInt(); // 3, 3, 8 
    } 

    /* 
    //paints the square with the variables generated in the random methods 
    */ 
    public void paint(Graphics g){ 

     g.setColor(RandomColour); 
     g.fillRect(XLocation,YLocation,Size,Size); 
    } 



    /* 
    //sets the squareWidth and square fallSpeed to a random value for every square created 
    */ 
    public Square(){ 
     generateRandomXLocation(); 
     generateRandomSquareSize(); 
     generateRandomFallSpeed(); 

    } 



    public void update(){ 

     //changes the squares xLocation and fallSpeed if the created square reaches the bottom of the screen 
     if(YLocation >= Game.WINDOW_HEIGHT){ 
      generateRandomXLocation(); 
      generateRandomFallSpeed(); 
      generateRandomSquareSize(); 
      YLocation = -Size; 
     } 

     //moves the square down if the square is inside the window 
     if(YLocation <= Game.WINDOW_HEIGHT){ 
      YLocation += fallSpeed; 
     } 
    } 


    public Rectangle GetBounds(){ 
     Rectangle rectangle = new Rectangle(XLocation,YLocation,Size,Size); 
     return rectangle; 
    } 
} 
+3

僅供參考:__從方塊開始的圓圈不聽起來不錯___。嘗試創建一個__Shape__類。然後從它繼承Square和Circle。 –

+0

爲了遊戲的緣故,它們都是墜落的物體,它很有意義。 – Karavi

+2

確切地說。 _「它們都是墜落的物體」或者「形狀」。不是正方形。圈子不能是方塊! –

回答

0

如果你想擁有一個子類,只要類A延伸B,其中B是超類。但是,如果我是你,我會做這樣的事情:

public abstract class Shape{ 
    protected int locX; 
    protected int locY; 
} 

public class Square extends Shape{ 
    //properties of Square 
} 

public class Circle extends Shape{ 
    //properties of Circle 
} 

甚至沒有任何繼承..

public abstract class ShapeProperties{ 
    protected int locX; 
    protected int locY; 
    //and any other members 
} 

public class Square{ 
    ShapeProperties sp; 
} 

public class Circle{ 
    ShapeProperties sp; 
} 

或..

public interface Positionable{ 
    public void getLocX(); 
    public void getLocY(); 
} 

public class Circle implements Positionable{ 
    //properties of Circle 

    @Override 
    public int getLocX(){ 

    }  

    @Override 
    public int getLocY(){ 

    } 

    public Rectangle getBounds(){ 
     return new Rectangle(getLocX(), getLocY(), size, size); 
    } 
} 

如果您打算將Circle擴展到Square,則爲reaso n獲取圓對象的邊界(或hitbox)。你可以只寫一個getBounds方法:

public class Circle{ 
    //properties of Circle 

    public Rectangle getBounds(){ 
     return new Rectangle(locX, locY, size, size); 
    } 
} 
0

我認爲你正在試圖做的是錯誤的做法。

如果您希望Square和Circle具有相同的屬性,則應該創建一個名爲Shape的接口,該接口具有要實施的屬性,然後只需執行public class Square implements Shapepublic class Circle implements Shape,然後在Square和Circle中實現屬性。

如果你想在所有的形狀只是一個實現,那麼在創建你上面提到的接口也將創建一個抽象類ShapeBase的頂部,你的班會,然後像public class Square extends ShapeBase implements Shapepublic class Circle extends ShapeBase implements Shape您的通用實施將在完成ShapeBase類和任何特定的實現都將在Square和Circle中完成。