[英]How could I use polymorphism here to improve my code?
I've read around that instead of using instanceof
and getClass
methods to implement polymorphism, as it's better object-oriented practice. 我已经读完了它,而不是使用instanceof
和getClass
方法来实现多态,因为这是更好的面向对象的实践。
If my program looks like the following: 如果我的程序如下所示:
Piece
SlowPiece extends Piece,
FastPiece extends Piece,
SlowFlexible extends SlowPiece,
FastFlexible extends FastPiece
And I have the following rule: SlowPiece's can only move one space, and FastPiece's as many as they'd like.
我有以下规则: SlowPiece's can only move one space, and FastPiece's as many as they'd like.
How would I use polymorphism
to ensure that if I'm moving a Piece like Piece piece1 = new SlowPiece();
我将如何使用polymorphism
来确保是否要移动像Piece piece1 = new SlowPiece();
一样的Piece piece1 = new SlowPiece();
that it is not moved more than one? 它不会移动超过一个?
I've got a class for the game-board, and it should be able to move the piece, but I'm not sure how to stop it from moving a SlowPiece more than 1 without code-sniffing. 我在游戏板上有一个类,它应该能够移动棋子,但是我不确定如何在没有代码嗅探的情况下阻止它移动SlowPiece超过1个。
Basically, I get how polymorphism is fantastic for say, listing things, but with actually interacting with them I don't get how to use it properly. 基本上,我知道多态性对于列出事物是多么神奇,但是实际上与它们进行交互时,我却没有如何正确使用它。
Make Piece
abstract and add an abstract method move()
to Piece
(you could also user an interface). 让Piece
抽象,并添加一个抽象方法move()
到Piece
(你也可以用户界面)。 Implement move()
differently (a SlowPiece
only moves 1 spot, etc). 不同地实现move()
( SlowPiece
仅移动1个点, SlowPiece
)。
public abstract class Piece {
public abstract void move();
}
public class SlowPiece extends Piece {
public void move() {
System.out.println("moving a slow piece");
}
}
public class FastPiece extends Piece {
public void move() {
System.out.println("moving a fast piece");
}
}
Piece f = new FastPiece();
f.move();
Piece s = new SlowPiece();
s.move();
Here, move()
has all the moving logic; 在这里, move()
具有所有移动逻辑; you could even use some overloads to allow for movement variety (like take N/S/W/E as arguments, etc). 您甚至可以使用一些重载来实现多种变化(例如,以N / S / W / E作为参数,等等)。
As another consideration to this problem, let's understand the basic overall concept. 作为对该问题的另一个考虑,让我们了解基本的总体概念。
Piece
can move()
. Piece
可以move()
。 Piece
move()
in a different manner than others. 某些类型的Piece
move()
与其他类型不同。 One can declare Piece
as an interface, and have all other Piece
s implement this. 可以将Piece
声明为接口,并让所有其他Piece
实现此接口。 This way, you're guaranteeing that every Piece
has a move()
, and also forcing you to ensure that every move()
is distinct, depending on the Piece
. 这样,您可以确保每个Piece
都有一个move()
,并且还强制您确保每个move()
都是不同的,具体取决于Piece
。
More than polymorphism you should probably think about responsabilities. 除了多态性之外,您可能还应该考虑责任。
You "board" class should do just something like 您的“董事会”班应该做的事情就像
piece.move();
and the piece should know how to move. 并且该作品应该知道如何移动。 So if you have like a dice you can pass to the move method the number and act consequently as 因此,如果您喜欢骰子,则可以将数字传递给move方法,然后按以下方式操作
piece.move(moves);
EDIT: 编辑:
the board should manage the movements, so let the move method return an int of the steps made by the piece. 电路板应该管理动作,因此让move方法返回一块所执行步骤的int值。 The movement should be managed from another class, like board, not from the piece itself. 应该从另一个类(例如板)来管理机芯,而不是从乐曲本身来管理。
I suggest you a very interesting book about OO Analysis: "Applying uml and patterns" by Craig Larman. 我建议您读一本有关OO分析的非常有趣的书:Craig Larman的“应用uml和模式”。 It contains also a step-by-step example of a board game as Monopoly! 它还包含一个棋盘游戏的示例,如Monopoly!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.