Reputation: 13
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.
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);
}
}
Upvotes: 1
Views: 337
Reputation:
You can use the model to generate code if you are poses a MDA approach. There's plenty tools that doing it effectively.
Working with interfaces you doesn't know what actual type of the object that this interface representing, in the rare case if you'd like to do so use instanceof
statement.
To create pure interfaces you don't put the variables inside it. Instead use private
members with mutators inside the implementing classes. If you want to access these properties through interface put these mutators to the interface as a public
methods.
Upvotes: 0
Reputation: 145
You can use a .toString() method in the for loop to determine the type of card, as long as you put a toString() method in each of the subclasses.
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();
}
Upvotes: 0
Reputation: 42431
I think you should learn about polymorphism. It goes strong when combined together with inheritance :)
The thing is that you shouldn't really know whether the card is a joker/normal card/etc. Instead you provide a method at the level of interface/abstract class that should handle a user event. All the "real" classes should override this method and provide an implementation. I'll show this:
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
}
}
Hope this helps
Upvotes: 5
Reputation: 2710
Use instanceof
if(someCard instanceof JokerCard) {
/* Some logic here*/
}
else if(someCard instanceOf ScoreCard) {
int score = ((ScoreCard) someCard).getScore();
/* some more logic here */
}
Upvotes: -1
Reputation: 691635
All the cards could for example have an int getScore()
method which would always return 0, except for ScoreCard instances.
All the cards could have a boolean canTake(Card otherCard)
which would always return true for JokerCard, and would obey some other rules for other cards.
It's hard to give a definitive answer, but if you want to use polymorphsm, then you need methods in the base class that must have an implementation is all the derived classes.
Upvotes: 2