user360968
user360968

Reputation: 65

How to make my objects interact properly in java

I have recently started learning java as my first ever step into object oriented programming and have been having trouble with having my objects interact properly, so I have a few questions, which seem to be incredibly simple but for some reason I cannot find the answers elsewhere:

Firstly, I have been trying to have one object read values from another, in this case it is an object called "game" reading attributes from multiple objects called "Item", the only way I can have the game object read any value from the item object is by having a method in the item class which returns the value which I want, and considering that each item has many attributes that I would like the game class to be able to read this simply does not seem like a particularly effective way to do this. So, is there any way to simply read values from an object? for example simply refering to the variable as item1.points, or item2.name, where item 2 is the object name and points is the variable name within the object?

Secondly, When I am assigning values to each item (I have the user assign certain values at the beginning of the game) is there any way to simply loop the same questions asking for values of attributes and have them assigned to my items sequentially? ie item1 then item2 then item3 rather than having selection statements to decide which item I an up to?

basically that is all I am having trouble with at the moment, any help would be greatly appreciated. Also, before anyone asks me to simply look it up, I HAVE been trying to look it up to no avail, however I may simply be using the wrong keywords to search this so any help in that regard would be much appreciated as well.

Thanks

Upvotes: 0

Views: 2013

Answers (4)

Wilson Freitas
Wilson Freitas

Reputation: 583

I agree with Kyle about your first question. The java bean standard is based on accessor methods: getters and setters. IDEs like Eclipse help a lot auto generating these methods for you.

About interacting over an object properties you can use the Apacha Commons Beanutils project to do that:

// Copy properties from someObject to myTargetObject
Map attributes = BeanUtils.describe(someObject);
SomeBean myTargetObject = new SomeBean();

for(String propertyName : attributes.keySet()){
  System.out.println("Value for attribute " + propertyName + " is " + attributes.get(propertyName));

  // Updates target object
  BeanUtils.setProperty(myTargetObject, propertyName, attributes.get(propertyName));
}

Upvotes: 1

slowpoison
slowpoison

Reputation: 616

For your first problem, make sure that both the classes are in the same package and the members are declared either public (not recommended), or protected (better) or don't specify any access specifier (which is a bit more restrictive than protected). If you follow that, both the objects belong to classes defined in the same package can access/modify each others' members without needing getters/setters.

For your second problem use reflection to go through members one by one. Look here: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

It will tell you how to find out about a class' methods, members etc. dynamically.

Upvotes: 0

PLane
PLane

Reputation: 186

Agree with Kyle. Also you could consider gathering those object.item_n properties into a property collection of some sort (like HashMap<String, String> textualproperties )and iterate over those or whatever.

Upvotes: 0

Kyle Sletten
Kyle Sletten

Reputation: 5413

The answer to your first question is yes any field marked public will be accessible directly, but that defeats the purpose of information-hiding, which is core to OOP. Your second question is a little bit more difficult. You can technically loop through the attributes on an object like that, but it requires you use reflection which will probably be more work than benefit.

Upvotes: 0

Related Questions