Results 1 to 3 of 3
Thread: Logical inheritance issue...
- 06-13-2011, 12:55 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 36
- Rep Power
- 0
Logical inheritance issue...
Hey...
I'm working on a small javagame but I ran into a wall. A small explanation: A character class, a number of subclasses starting with a warrior and a wizard. We also have an Item class with subclasses starting with ManaPotion and HealthPotion. All easy except for one thing: every character can carry items, no matter what kindoff item. But! only a wizard is allowed to use a mana potion. So I gave my superclass Character an abstract method useItem() but I don't have a single clue on how to implement this method in the warrior and wizard class so that only a wizard can use a ManaPotion. Also a HealthPotion logically restores health and not mana. (Mana is not a warrior variable but only used inside the wizard class) and so I'm also kinda stuck on how to make a difference between the 2 potions when using the useItem() method. I gave thus method a parameter 'Item item' so that an item can be assigned but this to gives that problem that when using this method, it also has to 'know' what kind off potion it is.
If anyone can put me in the right direction, I would be thrilled. :)
greetsLast edited by senca; 06-13-2011 at 01:03 PM.
- 06-14-2011, 04:03 AM #2
This is a really tough question to answer because the best solution depends on how much flexibility you want and how much time you're willing to spend writing it. At the simplest, you could have a big 2-dimensional array of booleans somewhere, indexed by character class and item number. For a bit more flexibility, you could give your character classes a canUse(Item item) method. Or you could give your item a isUsableBy(CharacterClass characterClass) method. Depending on your design, the second might make more sense.
Java Code:boolean isUsableBy(CharacterClass characterClass) { return characterClass.usesMana(); // or whatever }Get in the habit of using standard Java naming conventions!
- 06-14-2011, 11:00 AM #3
If all items have its own class, you can also use instanceof. Doing a quick google I found instanceof : Java Glossary that seems to explain it rather well.
Similar Threads
-
Inheritance issue
By powerpravin in forum New To JavaReplies: 3Last Post: 04-12-2011, 06:11 AM -
if block with logical or
By Ranu in forum New To JavaReplies: 6Last Post: 07-01-2010, 08:11 AM -
Logical Gates
By lingz89 in forum New To JavaReplies: 1Last Post: 08-17-2009, 01:11 AM -
Simple Inheritance issue...
By AWE in forum New To JavaReplies: 3Last Post: 07-27-2009, 09:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks