Sign in to follow this  
TimmerCA

[java] listing classes that inherit a particular class

Recommended Posts

TimmerCA    100
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
TimmerCA    100
[quote name='Danny02' timestamp='1311560110' post='4839812']
why do u want to write such ugly code?
can u describe to us what u want to achieve with this?
[/quote]

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
TimmerCA    100
[quote name='AAA' timestamp='1311133623' post='4837778']
Yes it is possible in Java. You can read up on Java Reflection to see how you can do it.
[/quote]

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:

[code]public class foo {
public class bar {
public int baz = 0;
}
}[/code]

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

[code]public class foo {
}

public class bar extends foo {
}[/code]

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
TimmerCA    100
[quote name='Danny02' timestamp='1311948015' post='4842148']
why not have one single resource class, which has as attributes a resource-type(enum) and a value.
[/quote]

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
Danny02    279
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
TimmerCA    100
[quote name='Danny02' timestamp='1311950745' post='4842168']
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.
[/quote]

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
rip-off    10976
[quote]
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
TimmerCA    100
[quote name='rip-off' timestamp='1311956385' post='4842210']
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.
[/quote]

Initially, it would be used during terrain generation when determining which resources appear on which map tiles. So, I'd iterate through the list of resources for each terrain tile to check to see which one was present at a given location. Here's some pseudo-Java-code:

[code]HashMap resources = new HashMap();
// load all the resource object types into this array

terrain_block tb[][] = new terrain_block[100][100];

for (x = 0; x < 100; x++) {
for (y = 0; y < 100; y++) {
tb[x][y] = new terrain_block();

foreach(resources as r) {
if (resource.exists_at(x, y)) {
tb[x][y].resource = r;
break;
}
}
}
}[/code]

Share this post


Link to post
Share on other sites
[quote name='Danny02' timestamp='1311560110' post='4839812']
why do u want to write such ugly code?
can u describe to us what u want to achieve with this?
[/quote]

I agree that there is more than likely a better way to achieve what he wants, but I dont think thats we should approach him in that manner as it comes off as rude.

@TimmerCA there are better way to implement something like this, I dont know your code so I wont be able to give you more then a very abstract suggestion. Not saying whatever you had in mind wont work, but there are more elegant ways to go about it, and why not be elegant when possible? itll make your code more readible and itll be easier to work with.

As for my suggestion.

When I come across something like this I always have some kind of Event/Trigger Packages. These Events and Triggers I program to contain some of the common actions and events that can happen during the game.

So i might have an interface called Event, which has a method called execute() which will execute whatever Event Inherits it, so that way for ANY event that happens they can all be execute the same way with the event class

after that I would Inhert the Event Interface into all the kind of events for example

DealDamageEvent
ChangeGraphicEvent

this are just some abtract ideas and names. Anyhow so basically we will have one class that checks for the triggers (conditions for the event to happen) and once it sees that a trigger has been pushed, then it executes its attached event.

So I might have something like this (psuedo style).....

ExplodingIronOre.attachEvent("on_destruction", new DealDamageEvent(50_HITPOINTS)); // I attach a DealDamageEvent to the Exploding Iron Ore that is told to deal
........

if(Player.Destroys(ExplodingIronOre)) // We check to see if the player destroyed the Exploding Ore Object we attached the event to
{
ExplodingIronOre.trigger("on_destruction", Player); // If they did destroy it lets execute the "on_destruction" Event, (If any were attached)
}
.......


ExplodingIronOre.trigger(String trigger_name, Entity focus)
{
.....
getEvent(trigger_name).execute(Entity); // one of the overloads of the execute class might allow you to specify a specific entity to execute the commands on
// in this case we are focusing it on the player who triggered the event
}


This is very abstract and I Hope you can kind of understand my methodology, I hope this was usefull. If you are interested more in this kind of design but are a little confused on how to implement its, Id be more then happy to help you If you PM me. Best of luck to you and your project.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this