如果楼主要采用结构题,建议采用这种形式。将Person的属性全部放到.m中,采用方法的形式给属性赋值,如果将属性放到.h中暴露出去,不安全!
#import <Foundation/Foundation.h>
typedef struct {
int year;
int mouth;
int day;
}Birthday;
typedef enum {
MAN,
WOMAN,
}SEX;
@interface CDPersons : NSObject
- (void)talk:(NSString*)words;
- (void)smile;
- (void)printMe;
- (void)initName:(NSString*)name;
- (void)initBirWithYear:(int)year Mouth:(int)mouth Day:(int)day;
- (void)initSex:(SEX)sex;
@end
#import "CDPersons.h"
@interface CDPersons()
@property(nonatomic,copy)NSString* name;
@property(nonatomic,assign)Birthday birthday;
@property(nonatomic,assign)SEX sex;
@end
@implementation CDPersons
- (void)talk:(NSString*)words{
NSLog(@"%@说%@",self.name,words);
}
- (void)smile{
NSLog(@"%@笑了",self.name);
}
- (void)printMe{
NSLog(@"我的名字%@--性别%u---生日%d%d%d",self.name,self.sex,self.birthday.year,self.birthday.mouth,self.birthday.day);
}
- (void)initName:(NSString*)name{
_name = name;
}
- (void)initBirWithYear:(int)year Mouth:(int)mouth Day:(int)day{
_birthday.year = year;
_birthday.mouth = mouth;
_birthday.day = day;
}
- (void)initSex:(SEX)sex{
_sex = sex;
}
@end
主函数
CDPersons* person0 = [[CDPersons alloc]init];
[person0 initName:@"chendong"];
[person0 initSex:MAN];
[person0 initBirWithYear:1993 Mouth:1 Day:10];
[person0 smile];
[person0 talk:@"中国人"];
[person0 printMe];
|