@interface Cattle : NSobjecs {
int legsCount;
@private
bool gender; //male = YES female = NO
@protected
int eyesCount;
@public
NSString *masterName;
}
- (void)saySomething;
- (void)setLegsCount:(int) count;
- (id)initWithLegsCount:(int) theLegsCount
gender:(bool) theGender
eyesCount:(int) theEyesCount
masterName:(NSString*)theMasterName;
@end
@implementation Cattle
-(void) saySomething
{
NSLog(@"Hello, I am a cattle, I have %d legs.", legsCount);
}
-(void) setLegsCount:(int) count
{
legsCount = count;
}
-(id)init
{
[super init];
return [self initWithLegsCount:4
gender:YES
eyesCount:2
masterName:@"somebody"];
}
- (id)initWithLegsCount:(int) theLegsCount
gender:(bool) theGender
eyesCount:(int) theEyesCount
masterName:(NSString*)theMasterName
{
legsCount = theLegsCount;
gender = theGender;
eyesCount = theEyesCount;
masterName = theMasterName;
return self;
}
@end
@implementation Bull
-(void) saySomething
{
NSLog(@"Hello, I am a %@ bull, I have %d legs.", [self getSkinColor],legsCount);
NSLog(@"I have %d eyes, my master is %@.", eyesCount,masterName);
//List below is illegal
//NSLog(@"My gender is %@",gender ? @"male" : @"female");
}
-(NSString*) getSkinColor
{
return skinColor;
}
- (void) setSkinColor:(NSString *) color
{
skinColor = color;
}
@end
#import "Bull.h"
#import "Cattle.h"
#import "MyNSobjecs.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Bull *redBull = [[Bull alloc] initWithLegsCount:4
gender:YES
eyesCount:2
masterName:@ "that cowboy"];
[redBull setSkinColor:@ "red"];
[redBull saySomething];
//legal, but not good
redBull- >masterName = @"that cowgirl";
//legal, but bad
//redBull- >eyesCount = 3;
//Trying to access a private ivar, VERY bad thing
//MyClass bullClass = redBull- >isa;
bool *redBullGender = (bool *)(redBull) + 8;
NSLog(@ "My gender is %@",*redBullGender ? @"male" : @"female");
[pool drain];
return 0;
}