0

我对在objective-c 中实现oops 概念有疑问。在objective-c 中是否可以实现全变。如何在objective-c中实现多态性。请举例说明?

4

3 回答 3

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];
}
于 2012-10-24T16:23:32.590 回答
1

Every method, including class methods, is dynamic in Objective-C.

One very basic approach would be:


Declare the base interface:

@interface MONConstantColor : NSObject
- (UIColor *)color;
@end

Define the base implementation:

@implementation MONConstantColor
- (UIColor *)color { return /* ...do/ret something appropriate */; }
@end

Then create some variations:

@interface MONRedColor : MONConstantColor
@end

@implementation MONRedColor
- (UIColor *)color { return [UIColor redColor]; }
@end

@interface MONYellowColor : MONConstantColor
@end

@implementation MONYellowColor
- (UIColor *)color { return [UIColor yellowColor]; }
@end
于 2012-10-24T16:16:01.390 回答
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;
}

使用此链接作为参考 http://www.tutorialspoint.com/objective_c/objective_c_polymorphism.htm

于 2015-10-27T12:47:17.860 回答