objective-c : 构造类、继承及实例

今天,我们来实现如下UML,并作为objective-c的第二个例子,介绍如何自己构造类,实现继承,并如何实例化,赋值等:

 

            +--------------------------+
            |       Phone              |
            |--------------------------|
            | LCDSize                |
            | Color                      |
            +-------------------------+
            | SetLCDSize          |
            | SetColor               |
            | Performance        |
            +--------------------------+
               ^                           ^
               |                            |
               |                            |
   +-----------+-------------+       +-+--------------+
   |      miPhone            |       | mPearPhone     |
   |-------------------------|         |----------------|
   |                         |              |                |
   |                         |              |                |
   +-------------------------+       |                |
   +                         |             |                |
   |                         |             +----------------+
   |                         |             +                |
   |   Performance        |       |  Performance   |
   +-------------------------+       |                |
                                           +----------------+

 

代码实现如下:

#import <Foundation/Foundation.h>

typedef enum {
  RED,
  WHITE,
  BLACK
}PHONECOLOR;

NSString *getColorStr(PHONECOLOR c)
{
    switch(c)
    {
        case RED: return @"red";
        case WHITE: return @"white";
        case BLACK: return @"black";
        default: return @"no color";
    }
}

@interface Phone: NSObject
{
    float LCDSize;
    PHONECOLOR color;
}

-(void) SetLCDSize : (float)size;
-(void) SetColor : (PHONECOLOR)c;
@end

@implementation Phone
-(void) SetLCDSize : (float)size
{
    LCDSize = size;
}

-(void) SetColor : (PHONECOLOR)c
{
    color = c;
}

//empty
-(void) Performance
{
}
@end

//-----iPhone "is a" Phone. it has all feature what a phone posess
@interface iPhone: Phone
@end

@implementation iPhone
-(void) Performance
{
     NSLog(@"iPhone performance: LCD-%1.1f Color-%@", LCDSize, getColorStr(color));
}
@end

//----PearPhone "is a" Phone too.
@interface PearPhone: Phone
@end

@implementation PearPhone
-(void) Performance
{
    NSLog(@"Pear Phone performance: LCD-%1.1f Color-%@", LCDSize, getColorStr(color));
}
@end


int main(int argc, const char *argv[])
{
    PHONECOLOR mcolor = WHITE;
    id iphone;
    iphone = [iPhone new];
    [iphone SetLCDSize: 4.0];
    [iphone SetColor: mcolor];

    [iphone Performance];

    id pearphone;
    pearphone = [PearPhone new];
    [pearphone SetLCDSize: 4.5];
    mcolor = BLACK;
    [pearphone SetColor: mcolor];

    [pearphone Performance];       

 return (0);
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值