您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 入門練習:微博系統的類別設計與實作

編輯:iOS 開發綜合

這個練習示範如何用類別繼承來建模一個微博系統:先有通用的「人」,再延伸出「博主」,最後由「微博」聚合這些角色與內容屬性。它涵蓋了 property 宣告、description 重寫與 isKindOfClass 的型別檢查。

一、主程式

// main.m
#import <Foundation/Foundation.h>
#import "Person.h"
#import "BlogMaster.h"
#import "Microblog.h"

int main(int argc, const char * argv[]) {
    Person *person = [[Person alloc] init];
    [person showName];
    NSLog(@"%@", person);

    Person *man = [[Person alloc] init];
    [man showName];

    return 0;
}

二、Person:人的抽象

// Person.h
#import <Foundation/Foundation.h>

@interface Person : NSObject {
    NSString *_name;        // 名字
    int       _age;         // 年齡
    NSString *_birthday;    // 出生年月
    char      _sex;         // 性別
    NSString *_address;     // 所在地
    NSString *_emotion;     // 感情狀況
    NSString *_bloodType;   // 血型
}

@property NSString *name;
@property int       age;
@property NSString *birthday;
@property char      sex;
@property NSString *address;
@property NSString *emotion;
@property NSString *bloodType;

- (void)showName;

@end
// Person.m
#import "Person.h"

@implementation Person

- (void)showName {
    NSLog(@"打印人的名字");
}

- (NSString *)description {
    return [NSString stringWithFormat:
        @"名字=%@ 年齡=%d 出生日期=%@ 性別=%c 住址=%@ 感情狀況=%@ 血型=%@",
        _name, _age, _birthday, _sex, _address, _emotion, _bloodType];
}

@end

三、BlogMaster:繼承自 Person

// BlogMaster.h
#import "Person.h"

@interface BlogMaster : Person
@end
// BlogMaster.m
#import "BlogMaster.h"

@implementation BlogMaster

- (void)showName {
    NSLog(@"博主的名字");
}

@end

四、Microblog:微博內容

// Microblog.h
#import <Foundation/Foundation.h>

@interface Microblog : NSObject {
    NSString *_registerTime;           // 註冊時間
    NSString *_introductionAboutBlog;  // 簡介
    NSString *_blogContent;            // 博客內容
    NSString *_commentContent;         // 評論內容
    int       _commentAmount;          // 評論數量
    int       _praiseAmount;           // 點讚數量
    NSString *_publishTime;            // 發表時間
    int       _transpond;              // 轉發數量
}

@property NSString *registerTime;
@property NSString *introductionAboutBlog;
@property NSString *blogContent;
@property NSString *commentContent;
@property NSString *publishTime;
@property int commentAmount;
@property int praiseAmount;
@property int transpond;

@end
// Microblog.m
#import "Microblog.h"

@implementation Microblog

- (NSString *)description {
    return [NSString stringWithFormat:
        @"博客內容=%@ 評論數量=%d 點讚數量=%d 發表時間=%@ 轉發數量=%d",
        _blogContent, _commentAmount, _praiseAmount, _publishTime, _transpond];
}

@end

五、幾個值得深究的設計點

  • 繼承的邊界在哪裡。BlogMaster 繼承 Person 是合理的——博主本質上就是人,只是多了一些行為。但若日後需要區分「一般使用者」與「博主」,用繼承會讓類別層級迅速膨脹,這時改用具名的 role 屬性或 protocol 會更合適。
  • 型別檢查的正確寫法。原範例中被註解掉的程式碼想判斷「這個 Person 是否其實是 BlogMaster」,正確寫法是 [person isKindOfClass:[BlogMaster class]],成立後才能安全轉型:
if ([person isKindOfClass:[BlogMaster class]]) {
    BlogMaster *master = (BlogMaster *)person;
    [master showName];
}
  • Microblog 應該持有作者。原範例把 BlogMaster *_blogMaster 成員註解掉了,導致微博與作者失去關聯。真實設計中,Microblog 應該有一個 Person *author 屬性,並在 initWithAuthor:content: 時注入,這樣才能正確表達「誰發了這條微博」。
  • 時間用 NSDate 而非 NSString。註冊時間、發表時間存成字串無法做排序與時間差計算,正式專案請用 NSDate 儲存,顯示時再格式化。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved