Objective-C边学边记-3:面向对象编程(OOP)基础

Objective-C中的OOP

 

@interface //定义类的公共接口


实例变量
{
    int a;
}
方法声明
- (void) draw;
- (void) setFillColor: (ShapeColor) fillColor;  //fillColor为方法参数
先行短线表明“这是新方法的声明”。(void)表明该方法不返回任何值。
方法名称是 setFillColor: ,结尾处的冒号表示后面会出现参数。
(ShapeColor) fillColor;
参数的类型是圆括号中指定的,紧随其后的是参数名称 

中缀符(infix notation):方法的名称及其参数都是合在一起的。例如:

1 [circle setFillColor: kRedColor];
2 [textThing setStringValue: @"hello there"
3 color:kBlackColor];

 

 


类实现
@implementation
@implementation是一个编译器指令, 表明你将为某个类提供代码。类名出现在@implementation之后。该行的结尾处没有分号,因为在Objective-C编译器指令后不必使用分号。

实例化(instantiation)
实例化对象时,需要分配内存,然后这些内存被初始化并保存一些有用的默认值,这些值不同于你在获得新分配的内存时得到的随即值。内存分配和初始化完成后,就创建了一个新的对象实例。

 

Example:

 

OBJECTIVE-C CODE   :No Title Code
  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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#import <Foundation/Foundation.h>

// *************************
// 可绘制的颜色枚举
typedef enum {
kRedColor,
kGreenColor,
kBlackColor
} ShapeColor;

// **************************
// 图形矩形
typedef struct{
int x , y , width , height;

} ShapeRect;

// **************************
// 转换颜色值枚举到字符串
NSString *colorName(ShapeColor color)
{
switch (color) {
case kRedColor:
return @"Red";
break;
case kGreenColor:
return @"Green";
break;
case kBlackColor:
return @"Black";
break;

}
return @"No this color";
} //colorName


#pragma mark *** Circle ***


// **************************
// Circle Inteface

@interface Circle : NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;

- (void) setBounds: (ShapeRect) bounds;

- (void) draw;

@end // Cricle


// **************************
// Circle类实现
@implementation Circle

- (void) setFillColor:(ShapeColor) c
{
fillColor = c;
}

- (void) setBounds: (ShapeRect) r
{
bounds = r;
}

- (void) draw
{
NSLog(@"draw a circle at ( %d %d %d %d ) in %@ .",
bounds.x, bounds.y, bounds.width, bounds.height,
colorName(fillColor));
}

@end

#pragma mark *** Rectangle ***

// Rectangle interface
@interface Rectangle : NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}
- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;

@end

// Rectangle Class
@implementation Rectangle
- (void) setFillColor:(ShapeColor) m_fillColor
{
fillColor = m_fillColor;

}

- (void) setBounds:(ShapeRect) m_bounds
{
bounds = m_bounds;
}

- (void) draw
{
NSLog(@"Draw a rectangle at ( %d %d %d %d ) in %@ color.",
bounds.x, bounds.y, bounds.width, bounds.height,
colorName(fillColor));
}

@end



#pragma mark *** Ellipse ***

// Ellipse Interface
@interface Ellipse : NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;

@end

// Ellipse Class
@implementation Ellipse

- (void) setFillColor:(ShapeColor) m_fillColor
{
fillColor = m_fillColor;
}

- (void) setBounds: (ShapeRect) m_Bounds
{
bounds = m_Bounds;
}

- (void) draw
{
NSLog(@"Draw a Ellipse at ( %d %d %d %d ) in %@ color.",
bounds.x, bounds.y, bounds.width, bounds.height,
colorName(fillColor));
}

@end


void DrawShapes(id shapes[],int count)
{
int i;
for(i = 0; i < count; i++)
{
id shape = shapes[i];
[shape draw];
}
}
// **************************
// Main
int main(int argc,const char *argv[])
{
id shapes[3];


ShapeRect rect0 = {
0,1,2,3 };
shapes[0] = [Circle new];
[shapes[0] setFillColor:kRedColor];
[shapes[0] setBounds: rect0];

ShapeRect rect1 = {
1,2,3,4 };
shapes[1] = [Ellipse new];
[shapes[1] setFillColor: kBlackColor];
[shapes[1] setBounds: rect1];

ShapeRect rect2 = {
2,3,4,5 };
shapes[2] = [Rectangle new];
[shapes[2] setFillColor: kGreenColor];
[shapes[2] setBounds: rect2];

DrawShapes ( shapes ,3 );

// [shapes[0] draw];
// [shapes[1] draw];
// [shapes[2] draw];
return 0;
}

 

继承

不要直接更改由继承得到的实例变量的值,一定要使用方法来更改它们。

 

OOP术语:
继承
复合
超类(superclass):是你所继承的类。
父类(parentclass): 是超类的另一种表达方式。
子类(subclass):是实施继承的类。
孩子类(childclass):是子类的另一种表达方式。
重写(override):改变方法的实现时,需要重写继承方法。
多态:使用更具体种类的对象代替一般类型的能力成为多态性(polymorphism)

self:每个方法调用都获得了一个名为self的隐藏参数,它是一个指向接收消息的对象的指针。方法使用self参数查找它们要使用的实例变量。
super:向super发送消息时,实际上是在请求Objective-C向该类的超类发送消息。

 

1 - (void) setFillColor: (ShapeColor) m_fillColor
2 {
3 if ( m_fillColor == kRedColor){
4 m_fillColor = kGreenColor;
5 }
6
7 [super setFillColor: m_fillColor];
8 }

 

posted @ 2010-10-25 00:04  Elf Sundae  阅读(956)  评论(3编辑  收藏  举报