2012-10-24 49 views
0

我在objective-c中實現oops概念中存在一個疑問。如何在objective-c中實現多態?請用例子解釋一下嗎?在ios中的多態性方法

+4

關於它的精彩文章:http://www.cocoawithlove.com/2008/03/polymorphism-is-always-wrong-word.html –

回答

1

Objective-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 
1
- (HomeWorkResult *)homeWorkResultFromHomeWorkTask:(HomeWorkTask *)task 
{ 
    if (!self.lazy) { 
     return [self HW_performHomeWorkTask:task]; 
    } 

    StackOverflowPost *post = [StackOverflow postHomeWorkTask:task]; 

    for (id user in post.responders) { 
     // Here is the pholyorphism[sic]. 
     // First, test to see if a stack overflow user is able to do home work tasks. 
     if ([user respondsToSelector:@selector(homeWorkResultFromHomeWorkTask:)]) { 
      // Next, have the user do the home work task. 
      HomeWorkResult *result = [user homeWorkResultFromHomeWorkTask:task]; 

      // If there is a result, return that result. 
      if (result) { 
       return result; 
      } 
     } 
    } 

    // Finally, if no stack overflow user does home work tasks or if there was no 
    // result perform the task yourself. 
    return [self HW_performHomeWorkTask:task]; 
} 
0

字多態性是指具有

Objective-C的多態性是指一個成員函數的調用將導致依賴於調用的函數的對象的類型來執行不同的功能多種形式

考慮這個例子,我們有一個Shape類,它爲所有的形狀提供了基本的接口。 Square和Rectangle從基類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; 
} 

使用這個鏈接作爲refernce http://www.tutorialspoint.com/objective_c/objective_c_polymorphism.htm