【问题标题】:Getting fields and methods from another class [duplicate]从另一个类中获取字段和方法[重复]
【发布时间】:2026-02-17 15:25:02
【问题描述】:

我是 Java 和面向对象的新手,如果不清楚,敬请见谅。

我已经使用 Env3D 制作了一个水族馆,并且想应用 SOLID 的原理

我有一个Simulation 班级,Token 班级,Fish 班级。

我的Token 课程目前处理生成鱼及其行为。 理想情况下,我希望该行为与 Token 类分开。

我需要我的Fish 来扩展两个类,TokenBehavior,我知道这是不可能的。

我该怎么做? Fish 可以实现Behavior 接口,但它如何从Behavior 类中获取字段和方法?

令牌类

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

【问题讨论】:

  • 假设Behavior 定义了鱼的行为(例如它如何移动),逻辑方法是将Behavior 作为一个单独的类并在@ 中有一个类似setBehavior 的方法987654338@。见Visitor-pattern。虽然没有看到Behavior 的实际代码,但很难分辨。
  • “我的令牌类目前处理生成鱼及其行为”如果这是您的目标,那么我不明白为什么 Fish 应该扩展这些类中的任何一个。该行为可以组成鱼类(作为类变量),Token 应该创建每个 Fish 并将其传递给它对应的行为。

标签: java object bluej


【解决方案1】:

Token 扩展Behavior,然后让Fish 扩展Token,因此您的Fish 类将继承TokenBehavior 的对象。这是解决问题的最佳方法在您的情况下存在多重继承问题。

作为替代方案,您的 Token 类看起来只是变量声明及其各自的 getter 和 setter,因此将 Token 设为接口可能不会太痛苦。

【讨论】:

    【解决方案2】:

    要实现解决方案的设计,您应该考虑“组合优于继承”原则(参见*的代码示例)。

    如果我理解你的问题,Fish 是一个令牌,我同意。 然而 Fish 不是一种行为,Fish 有一种行为。 然后你应该将 Behavior 对象聚合为 Token 属性,从中调用方法等。

    您还可以通过将 Behavior 定义为接口来添加另一个抽象级别,由具体类实现(比如 Behaviour1、Behaviour2)。 Token 类可以使用 Token 类中的附加抽象 getBehaviour() 方法访问行为对象,在 Fish 类中通过返回 Behaviour1 或 Behaviour2 实现的方法覆盖。

    【讨论】: