Results 1 to 4 of 4
  1. #1
    pfolder is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

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

    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.


    Java Code:
    public void renderWeapon(Weapon... weapons)
    	{
    		for(int i = 0; i < weapons.length; i++)
    		{
    			currentweapon = weapons[0];
    			((Axe) currentweapon).render(this);
    		}
    	}
    I have weapons[0] right now as an axe so the code works. I want to have it as

    Java 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.

  3. #3
    pfolder is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

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

    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.

Similar Threads

  1. getClass().getResource()
    By forwardbias in forum AWT / Swing
    Replies: 11
    Last Post: 03-29-2012, 03:10 AM
  2. Java getClass()
    By hqt in forum New To Java
    Replies: 2
    Last Post: 01-31-2012, 08:44 PM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  5. Replies: 2
    Last Post: 06-22-2010, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •