Hey guys, I couldn't really find an answer to this question because I don't really know how to ask this question because an example is just so much easier.

Basically, I want to have the current weapon a character has, and then whenever it changes it uses that new weapon's class' method for rendering. I need to get the class before it to use its rendering.

I have weapons[0] right now as an axe so the code works. I want to have it as

Code:

((weapons[0].class) currentweapon).render(this);

in order to have it get the class, but that code gives me an error. I also get an error if I use weapons[0].getClass()...so which code do I use?

Thanks for the help,
pfolder

07-07-2012, 01:18 AM

doWhile

Re: How to get the class of an object without getClass()?

I'm not clear what you are asking...does Weapon have a render method that accepts the current object? If so, then your first code listing should work without any class cast. If not - either create a class that contains this method in which all weapons inherit from, or create an interface with said method.

07-07-2012, 05:46 AM

pfolder

Re: How to get the class of an object without getClass()?

Both of them are from the class Items. The reason that I can't have a universal render is because each one has to be different or they have to take different parameters and I can't get that to work right.

07-07-2012, 06:27 AM

pbrockway2

Re: How to get the class of an object without getClass()?

Quote:

The reason that I can't have a universal render is because each one has to be different or they have to take different parameters and I can't get that to work right

No, you can declare a "universal render" in a superclass (or interface) and implement it appropriately in each concrete class like Axe. This is a *very* common way of doing things in Java. Think about a JButton instance and a JLabel instance: some common superclass declares the "universal render" - paintComponent() - but the two widgets implement this method very differently so that the right visual stuff gets painted onto the screen.

In the case of button/label the argument used is a Graphics instance which describes the context within which the rendering should take place. It is the caller's job to tell the "universal render" method the context within which it should render, it is not the caller's job to tell an Axe instance (for example) about the Axe specific stuff that needs to get rendered. If you find your render method relies on telling instances instance-specific information you should rethink you design. An Axe should "know" - ie keep track of - everything that is needed to render an axe in a given context. It should be "self contained" to that extent.

-----

I'm guessing other things besides weapons need to get rendered. Most likely what is needed here is a Renderable interface within which the "universal render" is declared. Concrete implementations - classes which implement Renderable - then implement this method in an appropriate manner. (They might be weapons, or rooms or anything else that needs to be rendered.) These concrete implementations can expect to be told the context within which the rendering should take place: but no-one should presume to tell an Axe instance how axes should be rendered.