一 概述
设计一些类,可以自行建立每个类之间的联系,尽量用面向对象的思想思考问题:
- 设计一个类表示一条微博,包含以下属性(发送时间属性可以忽略),微博内容,微博配图,发送时间,微博发送人,转发的微博,被评论数,被转发数
- 设计一个微博用户类,包含以下属性:姓名,微博号码,密码,头像,性别,手机,生日
二 类的设计
2.1 User类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| //User.h #import <Foundation/Foundation.h> //姓名,微博号码,密码,头像,性别,手机,生日 typedef enum { SexMan, SexWoman } Sex; typedef struct { int year; int month; int day; } Date;
@interface User : NSObject @property (nonatomic,retain) NSString *name; @property (nonatomic,retain) NSString *account; @property (nonatomic,retain) NSString *password; @property (nonatomic,retain) NSString *icon; @property (nonatomic,assign) Sex sex; @property (nonatomic,retain) NSString *phone; @property (nonatomic,assign) Date birthday; @end
//User.m #import "User.h" @implementation User - (void)dealloc { [_name release]; [_account release]; [_icon release]; [_password release]; [_phone release]; [super dealloc]; } @end
|
2.2 Status类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| //Status.h #import <Foundation/Foundation.h> #import "User.h" // 微博内容,微博配图,发送时间,微博发送人,转发的微博,被评论数,被转发数 @interface Status : NSObject @property (nonatomic,retain) NSString *text; @property (nonatomic,retain) NSString *icon; @property (nonatomic,assign) long time; @property (nonatomic,retain) User *user; @property (nonatomic,retain) Status *retweetStatus; @property (nonatomic,assign) int commentsCount; @property (nonatomic,assign) int retweetsCount; @end
//Status.m #import "Status.h" @implementation Status - (void)dealloc { [_text release]; [_user release]; [_retweetStatus release]; [_icon release]; [super dealloc]; } @end
|
2.3 main.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #import <Foundation/Foundation.h> #import "User.h" #import "Status.h"
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... User *u1=[[User alloc]init]; u1.name=@"zhangsan"; User *u2=[[User alloc]init]; u2.name=@"lisi"; Status *s1=[[Status alloc]init]; s1.text=@"今天天气真好!"; s1.user=u1; Status *s2=[[Status alloc]init]; s2.text=@"今天天气真的很好!"; s2.user=u2; [u2 release]; [u1 release]; [s2 release]; [s1 release]; } return 0; }
|