// Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property int age; //属性是_age
@end
//
// Person.m
#import "Person.h"
@implementation Person
- (instancetype)init
{
if (self = [super init]) {
_age = 10;
[self setAge:100];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"age = %i", _age];
}
@end
//
// Studnet.h
#import "Person.h"
@interface Studnet : Person
@property int no; // 学号, 属性是_no,
@end
//
// Studnet.m
#import "Studnet.h"
@implementation Studnet
- (instancetype)init
{
if (self = [super init]) {
// [self setAge:11];
_no = 1;
[self setNo:22];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"age = %i , no = %i,%i", [self age], _no,[self no]];
}
@end
//
// main.m
// 构造方法练习
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Studnet.h"
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc] init];
NSLog(@"%@", p);
Person *p1 = [[Person alloc] init];
NSLog(@"%@", p1);
Studnet *stu = [[Studnet alloc] init];
NSLog(@"%@", stu);
Studnet *stu1 = [[Studnet alloc] init];
NSLog(@"%@", stu1);
return 0;
}