I'm creating shooting game, and I created class Cowboy(abstract class, I will shoot cowboys). And I created 3 subclasses of that abstract class.
All cowboys have different guns, so I want, that cowboy1(who is with shotgun) would make more damage than cowboy2( who have a pistol). In my code, I have Cowboy c variable, for all three types of cowboys. I randomly assign to that 'c' variable different types of cowboys. So I need to know, which sublass I'm using now. I'm talking smth about 'instance of' in another languages. Does java has something similar to this?
How to know which subclass?
Page 1 of 16 Replies - 223 Views - Last Post: 07 August 2012 - 10:05 AM
Replies To: How to know which subclass?
#2
Re: How to know which subclass?
Posted 07 August 2012 - 08:39 AM
Java has the instanceof, but I don't really see why you'd need it for this use-case.
If you want to have Shoguns do more damage than Cowboys, just let Shogun's damage method return a higher value than that of Cowboy. That's what polymorphism is for.
If you want to have Shoguns do more damage than Cowboys, just let Shogun's damage method return a higher value than that of Cowboy. That's what polymorphism is for.
This post has been edited by sepp2k: 07 August 2012 - 08:40 AM
#3
Re: How to know which subclass?
Posted 07 August 2012 - 08:42 AM
why would you need to know? If your subclass methods are the same names as your super class, then the functionality of the sub class method will be used. That's the beauty of inheritance. For example, if you have a shoot() method in Cowboy, then PistolCowboy and RifleCowboy each have a shoot() method that you define using their own damage multiplier, but you still call shoot()
each shoot is defined differently so you have no reason to know what Cowboy is
Cowboy c; c = new PistolCowboy(); c.shoot(); c = new RifleCowboy(); c.shoot();
each shoot is defined differently so you have no reason to know what Cowboy is
#4
Re: How to know which subclass?
Posted 07 August 2012 - 08:42 AM
As a rule, if you find yourself using instanceof, you're doing something wrong. Not always true, but often enough to make you think twice.
#5
Re: How to know which subclass?
Posted 07 August 2012 - 08:55 AM
If you really find yourself customizing Weapons enough, I would consider creating an abstract Weapon class. Then have your abstract Character class store a Weapon. The details should then be left to the subclasses.
#6
Re: How to know which subclass?
Posted 07 August 2012 - 09:03 AM
Mac's got a good point. (in other news, water was seen to flow downhill earlier today, and a lawyer was found to be lying)
You might want to sketch out your design and your requirements a little. What sorts of things can a user do in this game?
for example, can a cowboy find a weapon and pick it up and use it? Or does a shotgun cowboy only ever get a shotgun?
You might want to sketch out your design and your requirements a little. What sorts of things can a user do in this game?
for example, can a cowboy find a weapon and pick it up and use it? Or does a shotgun cowboy only ever get a shotgun?
#7
Re: How to know which subclass?
Posted 07 August 2012 - 10:05 AM
Hello, I got another problem, so I will put it in this topic. I finished my project. With jar2exe wizard tool, I converted jar to .exe file. When I run game on my computer, everything is ok. But when I sent it to my friend, he said, that he can't open file, error message is "startup main class could not be found". What's wrong with this?
in my program I'm also using sounds, and
import sun.audio.*;
Is this can be the problem?
in my program I'm also using sounds, and
import sun.audio.*;
Is this can be the problem?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|