简体   繁体   English

如何在这里使用多态来改进代码?

[英]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. 我已经读完了它,而不是使用instanceofgetClass方法来实现多态,因为这是更好的面向对象的实践。

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. 作为对该问题的另一个考虑,让我们了解基本的总体概念。

  • A Piece can move() . Piece可以move()
  • Certain types of 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.

相关问题 我尝试使用多态性,但我的代码不能像我预期的那样工作,有人可以帮我解决这个问题吗? - I try to use polymorphism but my code does not work as i excpected, could anyone help me solve this problem? 我如何在这里整合执行者? 或者我该如何改善这个线程池? - How could I integrate executors here? Or how could I improve this threading pool? 我的插件代码减慢了Eclipse的速度,我该如何改进? - My plugin code slows down Eclipse, what could I improve? 我如何改善此任务的代码 - how can I improve my code for this task 我在不知不觉中利用多态吗? - Am I making use of Polymorphism without knowing here? 我怎么能在这里重构这段代码以获得相同的结果? - How could I refactor this bit of code here to achieve the same result? 如何避免在这里使用instanceof /如何使用多态 - How to avoid using instanceof / how to use polymorphism here 我无法通过我的代码点击Button元素,任何人都可以在这里帮忙 - I am unable to click on Button element through my code, could anybody help here 如何改进我对 Android SharedPreference 的使用? - How can I improve my use of Android SharedPreference? 关于如何使用我这里的代码创建“对象碰撞检测”的任何想法? - Any ideas on how i could create a “Object collision detection” with the code i have here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM