Jump to content
  • Advertisement
Sign in to follow this  
TimmerCA

[java] listing classes that inherit a particular class

This topic is 2493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have an abstract class called gameResource. Several other classes extend this class, such as gameResourceTree and gameResourceGold.

I'd like to have one single function that would create a HashMap or something similar of all the classes that extend gameResource so that I can iterate through each class. All the sub-classes will exist in the same package, and therefore be loaded in the same JAR file.

I'd like not to have to maintain this list of sub-classes manually. Is this possible in Java?

Share this post


Link to post
Share on other sites
Advertisement
Yes it is possible in Java. You can read up on Java Reflection to see how you can do it.

Share this post


Link to post
Share on other sites

why do u want to write such ugly code?
can u describe to us what u want to achieve with this?


Putting aside the fact that you haven't actually seen my code, can you explain to me how my code is ugly?

How would you implement a world full of resources that players can interact with, if not by inheriting some common functionality from a base class?

Share this post


Link to post
Share on other sites

Yes it is possible in Java. You can read up on Java Reflection to see how you can do it.


I did some reading about Java Reflection, but I'm not clear on how to do this still.

I looked at the getDeclaredClasses method, but that only appears to return classes that are defined inside a class, for example:

public class foo {
public class bar {
public int baz = 0;
}
}


In this case, if I called foo.class.getDeclaredClasses() I would get a list that included "bar". But, if I define these two classes:

public class foo {
}

public class bar extends foo {
}


In this case, foo.class.getDeclaredClasses() does not return "bar".

Or am I not understanding something?

Share this post


Link to post
Share on other sites
why not have one single resource class, which has as attributes a resource-type(enum) and a value.

Share this post


Link to post
Share on other sites

why not have one single resource class, which has as attributes a resource-type(enum) and a value.


Because each different type of resource behaves differently. They also all will have their own "draw" procedure; some will be done procedurally, and others drawn from a file created on Blender or whatever.

Share this post


Link to post
Share on other sites
You have to give us more information about what u are doing. How do resources "behave" in anyway?

You should always favor composition over inheritance, so if you are having a lot of classes with the same base class there is probably something u can do about it.

Share this post


Link to post
Share on other sites

You have to give us more information about what u are doing. How do resources "behave" in anyway?

You should always favor composition over inheritance, so if you are having a lot of classes with the same base class there is probably something u can do about it.


Well, for example:

When you harvest an apple tree, you get apples and the "draw" routine stops drawing the apples on the tree for a while thereafter.

When you harvest fish, you get fish, and maybe sometimes some other junk that got caught on your hook. Harvesting fish does not really change the visual representation of the resource, though.

When you harvest (let's say) exploding iron ore, you might get some iron or the iron node might explode, causing some health damage to you. In the case of exploding iron being successfully harvested, the resource would be drawn without iron ingots for a while. In the case where it exploded, it might draw a little crater for a while until the node re-spawns.

Some of the behaviors could be much more complex. For example, harvesting an ultra-rare resource much trigger a new quest for you, or it might cause several enemies to spawn right around the resource node to defend it. It might cause a warning to be sent to whichever faction controls the area to let them know that someone is pilfering their resources. It might cause you to be magically transported to a dungeon for some reason.

Share this post


Link to post
Share on other sites

Id like to have one single function that would create a HashMap or something similar of all the classes that extend gameResource so that I can iterate through each class.
[/quote]
How are you going to use this HashMap? If you can show us how you plan to use this collection, we'd be in a better place to help you build it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!