私はJavaとオブジェクト指向を初めて熟知しています。別のクラスのフィールドとメソッドを取得する
私はEnv3Dを使用して水族館を作って、私はSimulation
クラス、Token
クラス、Fish
クラスのを持っているSOLID
の原則を適用したいしています。
私のToken
クラスは現在、魚の生成とその動作を扱っています。 理想的には、この動作をToken
クラスとは別のクラスにします。
Fish
は私が知っているToken
とBehavior
の2つのクラスを拡張する必要があります。
どうすればよいですか? 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'を別のクラスとして残し、 'Fish'に' setBehavior'のようなメソッドを持たせることです。 [Visitor-pattern](https://en.wikipedia.org/wiki/Visitor_pattern)を参照してください。 「行動」の実際のコードを見ることなく、わかりにくい。 – Paul
"私のトークンクラスは現在、魚の生成とその振る舞いを扱っています"これがあなたの目標なら、なぜ魚がこれらのクラスを拡張すべきかわかりません。その振る舞いは、魚クラス(クラス変数として)を構成することができ、 'Token'はそれぞれの' Fish'を作成し、それに対応する振る舞いを渡すべきです。 – moondaisy