This site requires JavaScript, please enable it in your browser!
Greenfoot back
quakster1
quakster1 wrote ...

2014/5/22

Subclass problems

quakster1 quakster1

2014/5/22

#
I just want to know if there is a way i can create a subclass from the actor class then make another subclass underneath the one i just made. ex: Actor hero warrior priest i want to get the code from the hero class because i was hoping to create multiple classes underneath it. and all of them should have the move code. It just seem insufficient having to copy and paste every code that needs to be in the heroes. To summarise it, is there a way i can get code from "hero" and use it in "warrior" and "priest" without having to copy and paste the same code
quakster1 quakster1

2014/5/22

#
i dont see how this helps me
Jonche Jonche

2014/5/22

#
Well, as same as the methods that are in the Actor class, your heroes will have direct access to the methods described in the Hero class as long as the methods described in there have the "public" status. Which means that for example if you have a "public void move" method which causes your characters to move on the map, you will be able to call this method IN the priest class, but the method itself is in the Hero class. Practically it looks like this :
/**In the Hero class:  **/

public void move(int x) {
setLocation(getX() + x, getY());
}

/**This method is not a good move method. but it is enough for the example ^^.  **/


/**In the priest class you could write this :  **/


public void act()
{
move(5);
}
11frendash 11frendash

2014/5/22

#
for example if you need the two classes to come off of theherp class you need to put at the top before any programming YOU need to put 'public class hero extends warrior' and for your 'priest' class you replace the 'worrior for 'priest'. after YOU'VE done that you can programme like normal. P.S. please let me know if this is the right advice :)
danpost danpost

2014/5/22

#
11frendash wrote...
P.S. please let me know if this is the right advice :)
Right idea. Wrong code. If the two classes need to use code in the Hero class then Warrior extends Hero Priest extends Hero ( not 'hero extends warrior' ). Any code both Warrior and Priest need to use can be placed in the Hero class alone.
11frendash 11frendash

2014/6/2

#
danpost wrote...
11frendash wrote...
P.S. please let me know if this is the right advice :)
Right idea. Wrong code. If the two classes need to use code in the Hero class then Warrior extends Hero Priest extends Hero ( not 'hero extends warrior' ). Any code both Warrior and Priest need to use can be placed in the Hero class alone.
thanks needed to know, and yeah sorry wrong way around ha
You need to login to post a reply.