1

I think I miss some concepts of the OOP programming, but I don't know what exactly.

How Can I use the objects created from different classes?

For example, let's say I have to work with cards. There is a Card class, it contains all the shared properties. Normal cards and joker cards are inherited from the Card class (joker can take over any card color) and the a card can worth nothing or worth points, so there is a ScoreCard inherited from NormalCard.

UML:

So how can I use this model when I'm programming? I create N card in an array and when generating cards should I decide whether the actual card is a Joker/Score/Normal Card? And later how can I test if a card is a joker/normal/score card? Because if the ScoreCard have a private int score; and setters/getters the NormalCard is not going to have this property, so when I write an if statement I don't know what to test.

   Card[] cardsArray = new Card[52];

   for (int i = 0; i<cardsArray.length;i++) {
       //Some source tells if a card is a Score/Normal/Joker
       String src;
       switch (src) {
           case "Joker":Card[i] = new JokerCard();
               break;
           case "Normal":Card[i] = new NormalCard();

               .... etc
       }
   }

   //Some Userevent:
   ..userevent(Card in) {
        //Test what
        if (in.value == 4) {
            this.user.setScore(this.user.getScore()+in.score);
        }

    }
4

5 回答 5

5

我认为你应该学习多态性。当与继承结合使用时,它会变得强大:)

问题是你不应该真正知道这张牌是否是一张百搭牌/普通牌/等等。相反,您提供了一个接口/抽象类级别的方法,该方法应该处理用户事件。所有“真正的”类都应该覆盖这个方法并提供一个实现。我会展示这个:

interface Card {
    void handleUserEvent(UserEvent event);

}

public class NormalCard implements Card {
    public void handleUserEvent(UserEvent event) {
          // do something here
    }

}

public class Joker implements Card {
    public void handleUserEvent(UserEvent event) {
         // hey I'm a joker
    }

}

希望这可以帮助

于 2013-03-24T11:20:55.467 回答
2

例如,所有卡片都可以有一个int getScore()总是返回 0 的方法,除了 ScoreCard 实例。

所有的卡片都可以有一个boolean canTake(Card otherCard)对于 JokerCard 总是返回 true 的卡片,并且对于其他卡片会遵守一些其他规则。

很难给出一个明确的答案,但是如果你想使用多态,那么你需要基类中的方法,必须有一个实现是所有派生类。

于 2013-03-24T11:19:43.403 回答
0

您可以在 for 循环中使用 .toString() 方法来确定卡片的类型,只要在每个子类中放置一个 toString() 方法即可。

Card[] cardsArray = new Card[52];

   for (int i = 0; i<cardsArray.length;i++) {
       //Some source tells if a card is a Score/Normal/Joker
       String src = Card[i].toString; // This should define the type of current card
       switch (src) {
           case "Joker":Card[i] = new JokerCard();
               break;
           case "Normal":Card[i] = new NormalCard();

               .... etc  
       }
   }


public int getScore(Card c){
    // Returns the score. In the getScore() method you should already tell if the 
    //  card is an Ace or a Joker because you can give it a number of 0 for an Ace or whatever you want.
    int Score = c.getScore();

}
于 2013-03-24T11:27:29.130 回答
0

如果您采用 MDA 方法,则可以使用该模型生成代码。有很多工具可以有效地做到这一点。

使用接口时,您不知道该接口表示的对象的实际类型,在极少数情况下,如果您想这样做,请使用instanceof语句。

要创建纯接口,您不要将变量放入其中。而是private在实现类中使用带有 mutators 的成员。如果您想通过接口访问这些属性,请将这些修改器作为public方法放入接口。

于 2013-03-24T11:33:36.790 回答
-1

使用实例

if(someCard instanceof JokerCard) {
  /* Some logic here*/
}
else if(someCard instanceOf ScoreCard) {
  int score = ((ScoreCard) someCard).getScore();
  /* some more logic here */
}
于 2013-03-24T11:20:43.733 回答