Objective-C边学边记-3:面向对象编程(OOP)基础
时间:2010-10-25 来源:Elf Sundae
@interface //定义类的公共接口。
实例变量
{
int a;
}
方法声明
- (void) draw;
- (void) setFillColor: (ShapeColor) fillColor; //fillColor为方法参数
先行短线表明“这是新方法的声明”。(void)表明该方法不返回任何值。
方法名称是 setFillColor: ,结尾处的冒号表示后面会出现参数。
(ShapeColor) fillColor;
参数的类型是圆括号中指定的,紧随其后的是参数名称
中缀符(infix notation):方法的名称及其参数都是合在一起的。例如:
[circle setFillColor: kRedColor];
[textThing setStringValue: @"hello there"
color:kBlackColor];
类实现:
@implementation
@implementation是一个编译器指令, 表明你将为某个类提供代码。类名出现在@implementation之后。该行的结尾处没有分号,因为在Objective-C编译器指令后不必使用分号。
实例化(instantiation)
实例化对象时,需要分配内存,然后这些内存被初始化并保存一些有用的默认值,这些值不同于你在获得新分配的内存时得到的随即值。内存分配和初始化完成后,就创建了一个新的对象实例。
Example:
#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向该类的超类发送消息。
- (void) setFillColor: (ShapeColor) m_fillColor
{
if ( m_fillColor == kRedColor){
m_fillColor = kGreenColor;
}
[super setFillColor: m_fillColor];
}