#import <Foundation/Foundation.h>
@interface Person:NSObject{
NSString *name;
int age;
}
- (NSString *)name;
- (void) setName: (NSString *) newName;
- (int)age;
- (void) setAge: (int) newAge;
//actions
- (BOOL) isAllowedToVote;
- (void castBallot;
@end
Person.m
#import "Person.h"
@implementation Person
- (int) age{
return age;
}
- (void) setAge: (int) value{
age = value;
}
//... and other methods
@end
Using a class
id anObject = [[SomeClass alloc] init];
- (id) init
{
self = [super init];
if(self){
//initializations
}
return self;
}
- (Person *) createPersonWithName: (NSString *) aName{
Person *aPerson = [[Person alloc] init];
[aPerson setName:aName];
[aPerson setAge:21];
return aPerson;
}
Class Inheritance
@interface Student : Person {
NSString *major;
}
@end
@implementation Student
// Constructor for a student
- (id) init
{
self = [super init];
if(self != nil){
//set attributes
[self setMajor:@"Undefined"];
}
return self;
}
@end
No comments:
Post a Comment