 |
 |
Encapsulation?
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2007
Status:
Offline
|
|
I am reading "Cocoa programming for MAC OS X" by Aaron Hillegas. I am very concerned with what I have just read....
"Objective-C is a very simple language. It has no visibility specifiers. All methods are public, and all instance variables are protected. (Actually, there are visibility specifiers for instance variables, but they are rarely used. The default is protected, and that works nicely.)"
What? Can this really be true? I have been programming OOP for years now, and I believe encapsulation is one of the most important parts. Can someone explain this to me please? I would really like to understand this reasoning. An example of how to make instance variables private / public would be appreciated too.
Thanks
Pete
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2005
Location: Vancouver, BC
Status:
Offline
|
|
Correct me if I'm wrong, but aren't private instance variables one of the most important aspects of encapsulation?
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
[codex]@interface Thing : NSObject {
int number; // Only Thing and its subclasses can directly touch this
NSString *name; // Only Thing and its subclasses can directly touch this
@private
NSString *description; // Only Thing can directly touch this
@public
float hairLength; // Everybody can directly access this as aThing->hairLength
}
@end[/codex]
In practice, instance variables are almost never accessed directly outside getters and setters, even by the class that declares them. If you read The Objective-C Programming Language, you'll see that encapsulation is one of the big ideas behind Objective-C. This is similar to how Ruby does it, except Ruby doesn't have syntax for making an instance variable public.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
The other way to do private methods is to put them in a category in the .m file. It's still not *enforced*, but it's at least not in the header.
Private instance vars, on the other hand, I don't find very useful. ivars should never be accessed directly except in really weird edge cases.
(Last edited by Catfish_Man; Jun 17, 2007 at 01:56 AM.
)
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |