<pre name="code" class="objc">#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
int _no;
}
//_age的get和set方法的声明
- (void)setAge:(int)age;
- (int)age;
//_no的get和set方法的声明
- (void)setNo:(int)no;
- (int)no;
//构建函数的声明
- (id)init;
//自定义构建函数的实现
- (id)initWithAge:age andNo:no;
@end
@implementation Person
//_age的get和set方法的实现
- (void)setAge:(int) age
{
_age = age;
}
- (int)age
{
return _age;
}
//_no的get和set方法的声明
- (void)setNo:(int) no
{
_no = no;
}
- (int)no
{
return _no;
}
//构建函数的实现
- (id)init
{
if(self = [super init])
{
_age = 10;
_no = 1000;
}
return self;
}
//自定义构建函数的实现
- (id)initWithAge:age andNo:no
{
if(self = [super init])
{
_age = age;
_no = no;
}
return self;
}
@end
@interface Student:Person
{
NSString *_name;
}
//_name的get和set方法的声明
- (void)setName:(NSString *)name;
- (NSString *)name;
//构建函数的声明
- (id)init;
objective-c 构造函数
最新推荐文章于 2017-04-06 10:14:00 发布