Skip to main content
  1. About
  2. For Teams
Asked
Viewed 82 times
0

I have a class structure:

class BonusCard{
}

class AddResourceCard extends BonusCard{

}

class AddGoldCard extends BonusCard{

}

Now, I also have a function in which I want to pass AddResourceCard or AddGoldCard and in someones Inventory I want to check if an object in that Inventory is an instance of the class that I put into the function.

private void removeCardFromPlayer(Player player, BonusCard cardToRemove){
    for(BonusCard card : player.getInventory().getBonusCards()){
        if(card instanceof cardToRemove.getClass()){ //this line doesn't work sadly
            player.getInventory().getBonusCards().remove(card);
            break;
        }
    }
}

A function call should look like this:

removeCardFromPlayer(player, AddResourceCard);

or

removeCardFromPlayer(player, AddGoldCard);

There should be an easy straightforward way to accomplish what I'm trying to do, I just don't really know what to search for to be completely honest.

2
  • I would suggest not using classes for this, possibly consider an enum. That said. You need to include how you create the bonus card and how you create the cardToRemove.
    matt
    –  matt
    2022-05-11 10:10:53 +00:00
    Commented May 11, 2022 at 10:10
  • 1
    Either make it private void removeCardFromPlayer(Player player, Class<?>cardToRemoveClass) or don't use instanceof but rather card.getClass().equals(cardToRemove.getClass())
    Shark
    –  Shark
    2022-05-11 10:39:04 +00:00
    Commented May 11, 2022 at 10:39

3 Answers 3

3

I think what you are looking for is card.getClass().equals(cardToRemove.getClass()).

This compares the classes of the two objects and checks whether they are the same. If you want to regard some hierarchy, you would then probably rather go for isAssignableFrom instead of equals

Sign up to request clarification or add additional context in comments.

7 Comments

Glad to help. If you could accept the answer that would be great.
@DidseaJ I'm surprised that card.getClass().equals(cardToRemove.getClass()) returns true, but card instanceof cardToRemove.getClass() returns false. It would be good if you could create a complete example.
@matt the card instanceof cardToRemove.getClass() doesn't return false, it's simply not supported by the language. You would have to put the class you are checking against explicitly.
Oh true, they just wrote "doesn't work", I didn't realize it was not compiling. so .isInstance would be a good replacement. I also think isAssignableFrom is even better since equals will miss subclass.
I think, cardToRemove.getClass().isInstance( card); is the most straight forward. If you subclass card you might have BonusCards that do not have the same simple name.
|
1

You probably want to pass the class itself:

private void removeCardFromPlayer(Player player, Class<? extends BonusCard> typeToRemove) {
    for (BonusCard card : player.getInventory().getBonusCards()) {
        if (typeToRemove.isInstance(card)) {
            player.getInventory().getBonusCards().remove(card);
            break;
        }
    }
}

Calls to the method would look like this:

removeCardFromPlayer(player, AddResourceCard.class);
removeCardFromPlayer(player, AddGoldCard.class);

Comments

0

I would add new abstract method boolean canBeRemoved() to the parent class. And override and implement it in the child classes.

Then in the loop, I just will call this method and act accordingly.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.