[java] [SOLVED]Can i determin if a class has a particular method?

Started by
12 comments, last by relequestual 15 years, 1 month ago
Hi All. I'm currently working an a Java simulation game which works on a grid. I can place buildings and items on this grid. My current issue is, I'm using the same method to place both buildings and items on the grid. Problem is, the items can be rotated where as the buildings cannot (as they are square or rectangles drawn by dragging). My plan was to right click when building to rotate. I store the item or building that's currently being built in a class wide arraylist, which can hold any object. I'm doing this because each item type or room type has its own class. I'm still rather new to java, and so I'm still only using the one class within a class to capture the mouse clicks. Because of that, I was wondering if I could say something like if item in the arraylist has method x, then call method x, if not, do nothing. I know I'm probably doing a lot of things wrong here, so please do tell me. I have plans to upload my project when it reaches milestone 1. trying to keep it under wraps. Thanks for bothering to read. Thanks in advance for those who answer. [Edited by - relequestual on February 22, 2009 11:31:43 AM]
Advertisement

class Base
{
virtual bool CanRotate() { return false; };
};

class Item : public Base
{
virtual bool CanRotate() { return true; };
};
Quote:Original post by marcsh

class Base
{
virtual bool CanRotate() { return false; };
};

class Item : public Base
{
virtual bool CanRotate() { return true; };
};



Thanks for the reply.
I'm not sure what the : means in code.
also I don't know what a virtual bool is.

I think I get what your trying to say though.
I create a method for each of the building and item classes (or the relevant superclass) to return a boolean defining if it can rotate or not. Makes sense. I'm still getting the hang of the whole object orientation business. Learning on the fly.
public interface Rotatable {  void rotate(float angle);}


public class Building extends Item implements Rotatable {  void rotate(float angle) {    ...  }};for (Item i : items) {  if (i instanceof Rotatable) {    ((Rotatable)i).rotate(45.0f);  }}


However, instanceof usually indicates design problem. Better solution to above is to register buildings and objects that can be rotated elsewhere. So in addition to a list that has all objects, you store rotatable objects in a separate list as well:
List<Item> allItems;List<Rotatable> rotatables;


Then, when no casts are needed, and you end up with:
for (Rotatable r : rotatables) {  r.rotate(45.0f);}


The code snippt is C++

class Item : public Base// is equivalent topublic class Item extends Base

virtual bool CanRotate() { return false; };// is similar topublic abstract boolean canRotate();


For reference you can use reflection to check if a class contains certain methods.


However in this case it would probably be best to have a parent class or interface.

Since the buildings only rotate when clicked you could simply forward the click event (or wrap it in your own code) to the items. In the case of a building it would rotate; other items may do nothing in response.

public interface MyMouseListener {    public void rightMouseDown(final MouseEvent e);}public class Item implements MyMouseListener {    public void rightMouseDown(final MouseEvent e) {        // Do nothing.    }}public class Building extends Item {    @Override    public void rightMouseDown(MouseEvent e) {        // Invoke rotate method.    }}// Some where in another class there is a mouse listener.public void mousePressed(MouseEvent e) {    final boolean isRightMouseButton = (e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK;    if (isRightMouseButton) {        // Identify which Grid item was clicked.        Item i;        // ...        final MyMouseListener m = (MyMouseListener) i;        m.rightMouseDown( e );    }}


[Edited by - Angex on February 20, 2009 1:14:11 PM]
Re Antheus

Sorry, first half of post went right over my head.
As for second part of post, I see what your saying.
I have an arraylist of items (all can be rotated) and an arraylist of rooms.

I'll explain my current class structure.

Item has
top left position (Point)
width, height (Int)

usableItem inherits Item has
a point of usage (Point)
rotation (int) which can be 1 2 3 or 4, as were on a grid.

ReceptionDesk inherits usableItem


At current, usable item deals with the rotation, adding one to the rotation and setting back to 1 if previous was 4. the ReceptionDesk class deals with setting the width and height (2 across, 1 down if rotation is 1, ect) and then locates the point of use.

I did some research before I started the project, and plan to use A* path finding (I hope), and thought i would need a point of usage for usable items and or access to doors of rooms.

Re Angex

Sorry I don't know any C++

I think I have done as you described, using the parent class Item. I have problems though as the Building class also inherits the Item class, although not all non Building items are usable. I couldn't think of another name for the class Item, other than "Thing".

I could probably do with an experienced programmer taking a look at my class structures. I did plan before hand, however there is lots I didn't think or realise. This is my first project with more than 3 classes, so I admit, I'm probably out of my depth, but I plan to learn on the fly with this one, and have no time scale. See it as an on going project.
Quote:Original post by relequestual
Re Antheus
Sorry, first half of post went right over my head.
At this point, you need to pick up a book on Java (your local library should have a couple), and read up on inheritance. Inheritance is a very important part of object-orientation in Java, so it is essential to have a solid understanding of the basics.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by relequestual
Re Antheus
Sorry, first half of post went right over my head.
At this point, you need to pick up a book on Java (your local library should have a couple), and read up on inheritance. Inheritance is a very important part of object-orientation in Java, so it is essential to have a solid understanding of the basics.


Thanks swiftcoder. I am currently reading a book on java recommended by the uni I'm currently studying at.I understand part of inheritance, however I'm not to sure what is meant by implements or instanceOf. I also don't understand using float angles. Luckily I don't need to under stand the angles for now as I'm using a grid system. I will indeed read up on the inheritance section. I thought that by using extends, it did everything I needed.
Marsch said that both of your items were able to be placed on a grid (AbleToBePlacedOnAGrid), some of which can be rotated (class Item extends AbleToBePlacedOnAGrid { boolean canRotate(){return true;} } and some of which cannot (class Building extends AbleToBePlacedOnAGrid { boolean canRotate(){return false;} }. Both Item and Building are AbleToBePlacedOnAGrid objects.

Antheus said that a Building is an Item (Building extends Item), and a Building is also a Rotatable (Building implements Rotatable) (he misread your post I think). The 'instanceof' operator tells you whether an object "is a" (some other object). If you were checking an object o that was really a Building, then: o instanceof Rotatable returns true, but if it was an Item, then that would return false.
Thanks for clearing that up Argus2 :)

re what marsch said, would I put that all on the same line?
How exactly would I go about implementing what they said there?

I should have a class AbleToBePlacedOnAGrid.
As Building inherits Item, would I negate the need for the Item class as such?
I say this because all the Item class has is the top left corner, height and width of an item. in essence, Item is my AbleToBePlacedOnAGrid.

Could I not set up a boolean in Item which defaults to false, however in the subclass usableItem say it equals true? or is that basically what is being said here?

Sorry for being a tad slow on this. Learning on the fly

This topic is closed to new replies.

Advertisement