Objective c ios中的多态性方法
我对在objective-c中实现oops概念有疑问。objective-c中是否可能出现光学现象。如何在objective-c中实现多态性。请举例说明?在objective-c中,包括类方法在内的每个方法都是动态的 一个非常基本的方法是:Objective c ios中的多态性方法,objective-c,ios,xcode4,Objective C,Ios,Xcode4,我对在objective-c中实现oops概念有疑问。objective-c中是否可能出现光学现象。如何在objective-c中实现多态性。请举例说明?在objective-c中,包括类方法在内的每个方法都是动态的 一个非常基本的方法是: 声明基本接口: @interface MONConstantColor : NSObject - (UIColor *)color; @end 定义基本实现: @implementation MONConstantColor - (UIColor *)c
声明基本接口:
@interface MONConstantColor : NSObject
- (UIColor *)color;
@end
定义基本实现:
@implementation MONConstantColor
- (UIColor *)color { return /* ...do/ret something appropriate */; }
@end
然后创建一些变体:
@interface MONRedColor : MONConstantColor
@end
@implementation MONRedColor
- (UIColor *)color { return [UIColor redColor]; }
@end
@interface MONYellowColor : MONConstantColor
@end
@implementation MONYellowColor
- (UIColor *)color { return [UIColor yellowColor]; }
@end
多态性这个词意味着有多种形式 Objective-C多态性意味着对成员函数的调用将导致根据调用函数的对象类型执行不同的函数 考虑这个例子,我们有一个类Shape,它为所有形状提供基本接口。正方形和矩形是从基类形状派生的 我们有方法printArea,它将展示OOP特性多态性
#import <Foundation/Foundation.h>
@interface Shape : NSObject
{
CGFloat area;
}
- (void)printArea;
- (void)calculateArea;
@end
@implementation Shape
- (void)printArea{
NSLog(@"The area is %f", area);
}
- (void)calculateArea{
}
@end
@interface Square : Shape
{
CGFloat length;
}
- (id)initWithSide:(CGFloat)side;
- (void)calculateArea;
@end
@implementation Square
- (id)initWithSide:(CGFloat)side{
length = side;
return self;
}
- (void)calculateArea{
area = length * length;
}
- (void)printArea{
NSLog(@"The area of square is %f", area);
}
@end
@interface Rectangle : Shape
{
CGFloat length;
CGFloat breadth;
}
- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;
@end
@implementation Rectangle
- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth{
length = rLength;
breadth = rBreadth;
return self;
}
- (void)calculateArea{
area = length * breadth;
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Shape *square = [[Square alloc]initWithSide:10.0];
[square calculateArea];
[square printArea];
Shape *rect = [[Rectangle alloc]
initWithLength:10.0 andBreadth:5.0];
[rect calculateArea];
[rect printArea];
[pool drain];
return 0;
}
#导入
@接口形状:NSObject
{
CGFloat区域;
}
-(无效)打印区域;
-(无效)a;
@结束
@实现形状
-(无效)打印区域{
NSLog(@“面积为%f”,面积);
}
-(无效)计算ea{
}
@结束
@界面正方形:形状
{
CGFloat长度;
}
-(id)初始侧:(CGFloat)侧;
-(无效)a;
@结束
@实施广场
-(id)初始侧:(CGFloat)侧{
长度=侧面;
回归自我;
}
-(无效)计算ea{
面积=长度*长度;
}
-(无效)打印区域{
NSLog(@“正方形的面积为%f”,面积);
}
@结束
@界面矩形:形状
{
CGFloat长度;
浮动宽度;
}
-(id)initWithLength:(CGFloat)rle长度和宽度:(CGFloat)rBreadth;
@结束
@实现矩形
-(id)initWithLength:(CGFloat)rle长度和宽度:(CGFloat)rBreadth{
长度=长度;
宽度=rBreadth;
回归自我;
}
-(无效)计算ea{
面积=长度*宽度;
}
@结束
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
形状*square=[[square alloc]initWithSide:10.0];
[平方计算ea];
[平方面积];
形状*rect=[[矩形alloc]
初始长度:10.0,宽度:5.0];
[rect calculateArea];
[矩形打印区域];
[泳池排水沟];
返回0;
}
使用此链接作为参考
关于它的精彩文章:
#import <Foundation/Foundation.h>
@interface Shape : NSObject
{
CGFloat area;
}
- (void)printArea;
- (void)calculateArea;
@end
@implementation Shape
- (void)printArea{
NSLog(@"The area is %f", area);
}
- (void)calculateArea{
}
@end
@interface Square : Shape
{
CGFloat length;
}
- (id)initWithSide:(CGFloat)side;
- (void)calculateArea;
@end
@implementation Square
- (id)initWithSide:(CGFloat)side{
length = side;
return self;
}
- (void)calculateArea{
area = length * length;
}
- (void)printArea{
NSLog(@"The area of square is %f", area);
}
@end
@interface Rectangle : Shape
{
CGFloat length;
CGFloat breadth;
}
- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;
@end
@implementation Rectangle
- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth{
length = rLength;
breadth = rBreadth;
return self;
}
- (void)calculateArea{
area = length * breadth;
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Shape *square = [[Square alloc]initWithSide:10.0];
[square calculateArea];
[square printArea];
Shape *rect = [[Rectangle alloc]
initWithLength:10.0 andBreadth:5.0];
[rect calculateArea];
[rect printArea];
[pool drain];
return 0;
}