• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method to get a single object attribute for multiple object instances.  RSS feed

 
Ian M Graham
Greenhorn
Posts: 3
Java Mac Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a simple question, hope I'm wording it the right way.

I've created a Parent class 'product' with subclasses 'book' and 'software.' The book and software class inherit an object attribute "description" from the 'product' class.

Let's say I construct several instances of a book and a software object, all with different values for "description". Is there a method I can use to gather the "description" String attribute for all my objects at once rather than running my getDescription method for each individual object?

Ideally I'd like to print them out one after another each on a new line in console.

What I invasion would be something like allObjects.getName();

Any help is appreciated thank you
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a Product array. Then when you want to print the descriptions, just use an enhanced for loop to run through your various instances.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian M Graham wrote:Is there a method I can use to gather the "description" String attribute for all my objects at once rather than running my getDescription method for each individual object?
Ideally I'd like to print them out one after another each on a new line in console.
What I invasion would be something like allObjects.getName();

Well, I hope you're not planning on invading anybody.

However, in answer to your question: No, there is no specific construct that means "all my objects", although the new lambda constructs in version 8 do allow you to apply a function to all objects in a collection. However, I suspect that's overkill for something like this.

You could emulate it by putting the loop that Joel suggests in a method, though. Then you just call that method to print out all your descriptions.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!