2017-03-13 24 views
1

我是新來的Java和麪向對象,所以如果我不清楚道歉。從另一個類中獲取字段和方法

我一直在使用Env3D作出了水族館,想申請的SOLID

的原則我有一個Simulation類,Token類,Fish類的。

我的Token類目前處理產生魚及其行爲。 理想情況下,我希望行爲是從Token類中分離出來的一類。

我需要我的Fish擴展兩個類,TokenBehavior,我知道這是不可能的。

我該怎麼做? Fish可以實現Behavior接口,但它如何從Behavior類獲得字段和方法?

Token類

package UserCode; 

/** 
* It's a Token! 
* 
* @author (Tom) 
* @version (02.03.2017) 
*/ 
public class Token 
{ 

// Env3d-defined object-specific fields: 
// Reference to the 3D model, called 'model': 
String model; 

// Reference to texture-map, called 'texture': 
String texture; 

// Scale factor applied to model: 
double scale; 

// Position in 3D world (x,y,z coordinates): 
double x; 

// Position in 3D world (x,y,z coordinates): 
double y; 

// Position in 3D world (x,y,z coordinates): 
double z; 

// Orientation (about x,y,z): 
double rotateX; 

// Orientation (about x,y,z): 
double rotateY; 

// Orientation (about x,y,z): 
double rotateZ; 

// Set transparency to true: 
boolean transparent=true; 


public void setModel(String model){ 
    this.model = model; 
} 

public void setScale(double scale){ 
    this.scale = scale; 
} 

public void setTexture(String texture){ 
    this.texture = texture; 
} 

public void move() 
{ 
    // rotate about y axis 
    //rotateY += 1; 
} 



public void setOrientationX(double x) 
{ 
    // set position 
    this.rotateX = x; 
} 

public void setOrientationY(double y) 
{ 
    // set position 
    this.rotateY = y; 
} 

public void setOrientationZ(double z) 
{ 
    // set position 
    this.rotateZ = z; 
} 





public void setPositionX(double x) 
{ 
    // set position 
    this.x = x; 
} 

public void setPositionY(double y) 
{ 
    // set position 
    this.y = y; 
} 

public void setPositionZ(double z) 
{ 
    // set position 
    this.z = z; 
} 
} 

JavaFish類

package UserCode; 

/** 
* <h1>Aquarium: JavaFish</h1> 
* <p>This class generates and controls the JavaFish in the Aquarium. 
* <p>The characteristics of the JavaFish are to swim horizontelly, backwards and forwards. 
* 
*@version 1.0.0 
*@author Tom 
*/ 

public class JavaFish extends Token 
{ 
private double _xspeed = 0.08; 

/** 
* Creates the JavaFish and initilises instance/object variables of images of JavaFish and Aquarium. 
* The second initilises position and orientation. These come from the  Framework package. 
*/ 

public JavaFish() 
{ 
    setModel("models/billboard/billboard.obj"); 
    setScale(0.5); 
    setTexture("textures/javaFish/JavaFish.png"); 


    setPositionX(1.0); 
    setPositionY(5.0); 
    setPositionZ(1.0); 

    setOrientationX(0); 
    setOrientationY(-90); 
    setOrientationZ(0); 
} 

public void move() 
{ 
    // JavaFish x-axis assigned to move forwards (+=) by instance varible in constructor 
    x += _xspeed; 

    // Flip JavaFish orientation and change direction if fish reaches specific x-axis point 
    if (x < 2) 
    { 
     _xspeed = 0.05; 
     setOrientationY(-90); 
    } else if (x > 8){ 
     _xspeed = -0.05; 
     setOrientationY(90); 
    } 
} 
} 
+0

假設行爲定義了魚的行爲(例如它是如何移動的),邏輯的方法是將行爲作爲一個單獨的類離開,並在'魚'中有一個像'setBehavior'這樣的方法。請參閱[訪問者模式](https://en.wikipedia.org/wiki/Visitor_pattern)。很難說,雖然沒有看到「行爲」的實際代碼。 – Paul

+0

「我的令牌類目前處理產生魚類和他們的行爲」如果這是你的目標,那麼我不明白爲什麼'魚類'應該擴展任何這些類。行爲可以組成魚類(作爲一個類變量),'Token'應該創建每個'Fish'並傳遞它是相應的行爲。 – moondaisy

回答

0

Token延長Behavior,然後有Fish延長Token,所以你Fish類將繼承來自TokenBehavior對象。這是最好的方式圍繞着你的情況下的多重繼承問題。

作爲一種替代方案,您的Token類看起來只是變量聲明和它們各自的getter和setter,所以使得Token成爲一個接口可能不會太痛苦。

0

爲了實現您的解決方案的設計,您應該考慮「構造優於繼承」原則(請參閱wikipedia中的代碼示例)。

如果我理解你的問題,魚是一個令牌,我同意。 但魚不是行爲,魚有行爲。 那麼你應該總體行爲的對象令牌屬性,調用從中方法等

您也可以通過定義行爲的接口,通過具體的類實現增加一些抽象的另一個層面(比方說Behaviour1,Behaviour2)。 Token類可以使用Token類中的附加抽象getBehaviour()方法訪問行爲對象,通過返回Behaviour1或Behaviour2實現的方法在Fish類中重寫。

相關問題