2017-03-13 83 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类中重写。

相关问题