• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method to retrieve all same instance properties of classes.

 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Let's say I have a class MyAnimal that looks like this:


Now let's say that I was able to get a list of instances of MyAnimal.


I want to create a method that when passed in the property as a string, will return an array of each instance's specific property. The thing is, I'm used to dot notation, so I don't know how to handle retreiving data when a string is given.

For example, let's say I have three animals and they have the colors red, green, and blue. When I call this method:

It should return:


I should also be able to call:


Thanks for the help!
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the reflection API ( http://download.oracle.com/javase/tutorial/reflect/index.html ) but since this is posted in the "Beginning Java" forum I suspect your requirement is the result of a bad design. Why do you think you need this?
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I have an Array of Objects, and I need to quickly print out and log (to a text file) all of the same instance properties for the Objects. I don't want to have to create a new method for each since I know specs are going to change and more instance variables will be added to the Class. I'd much rather call the same method with a different argument.
 
Ove Lindstr├Âm
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Filmer wrote:Thanks.

I have an Array of Objects, and I need to quickly print out and log (to a text file) all of the same instance properties for the Objects. I don't want to have to create a new method for each since I know specs are going to change and more instance variables will be added to the Class. I'd much rather call the same method with a different argument.



In my humble opinion, you are not thinking in an object oriented matter. You should split out the Color to a class of its own, maybe even an Enum if there is only to be a fixed number of colors. Also, the Skills/TrainedAreas could be a class of its own. I can bet quite big bucks that the later will have a training level later on...

Regarding using one method to get them all (aka the method of the ring) or multiple methods there are some different ways to go:
1. Live with the fact that you have to implement a new method for each attribute type.
2. Rely on reflection and make a nice bean centric framework.
3. Look into some sort of persistence layer and use that to search by attribute.
4. Delegate to the class holding the attribute to return is based on an interface that allows you to query by attribute name.
5. A combination of the previous four.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic