Results 1 to 4 of 4
- 09-08-2011, 10:55 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Modifying an object's attributes by using another object's method
Hello all, I have a question that most of you will probably laugh about. But for my own purposes I am currently learning Java, and as I am quite new to programming and OOP, this problem actually arouse:
Given, I have two objects: Controller c1 and Bulb b1. This Controller object has a method called "setBulb", and it should be setting a "Bulb"'s attribute "isOn" to either true or false.
How do I accomplish that? I can't do this in the "Controller"'s method, as the object b1 has not yet been created and is therefore unknown ...
I hope this is not totally nonsense. I'd really appreciate any suggestions, and if what I want to do is totally bad programming style, I am glad if you'd correct me.
Best Regards
Dennis
- 09-08-2011, 10:59 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Modifying an object's attributes by using another object's method
If a Controller needs to set a Bulb to on or off, it needs to have a Bulb; it can either create one or you can 'give' it one, otherwise the Controller doesn't have a Bulb and can't do anything ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-08-2011, 11:41 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Re: Modifying an object's attributes by using another object's method
Thanks for the swift reply, Jos. So I thought, but lets say there are about 30 Bulb objects that need to be handed over ... how do I put them in an array?
Best
Dennis
- 09-08-2011, 11:53 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Modifying an object's attributes by using another object's method
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
[Paremeters] in a method(Object expA, Object expB){}
By Unsub in forum New To JavaReplies: 2Last Post: 01-29-2010, 02:01 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Classes with object attributes
By RRasco in forum New To JavaReplies: 5Last Post: 11-25-2008, 12:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks