objective-c : 构造类、继承及实例
来源:互联网 发布:java中static什么意思 编辑:程序博客网 时间:2024/06/05 17:46
今天,我们来实现如下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);
}
- objective-c : 构造类、继承及实例
- Objective-C - 继承的经典实例
- Objective-C 类继承关系
- Objective-C 学习笔记 04 - 继承和对象的实例
- Objective-C - 封装、继承、多态综合实例
- C# 类继承,构造函数 实例
- objective-c 构造函数
- Objective-C - 构造方法
- 【Objective-C】构造方法
- Objective-C构造方法
- Objective-C 构造方法
- Objective-c构造函数
- Objective-C 类的继承、方法重载
- Objective-C 类的继承、方法重载
- objective-c语法学习:类的继承
- 【Objective-C】OC中类的继承
- 黑马程序员--- objective-c 类的继承
- Objective-c 对象与类 继承
- Ubuntu 开机自动挂载其他分区
- nsstring有个方法可以以某个单词截取
- 需求分析之数据流图学习
- 关于px,em,pt,ppi,dpi,dp,dip和sp
- CSDN BYEBYE
- objective-c : 构造类、继承及实例
- VS2008常用快捷键
- 设计模式之十二:组合模式(composite)
- 设置VS2008 快捷键 快速注释
- 黑马程序员_Java基础Day02(上)_Java语言基础(Done)
- OCJP之Container
- std::setw(size)与std::setfill(char)
- 图书管理系统
- 控制台程序 如何查看所有的输出结果