简体   繁体   English

Objective-C中的多态性-iOS

[英]Polymorphism in Objective-C - iOS

I have been using Polymorphism in C++ for a long time now and I rather like using it. 我已经在C ++中使用多态了很长时间了,我更喜欢使用它。 Does Objective-C have this functionality? Objective-C是否具有此功能? Maybe has something to do with Delegates? 也许与代表有关?

I have been playing around with iOS development for a while and have been using Frameworks such as MessageUI and iAd. 我从事iOS开发已有一段时间,并且一直在使用诸如MessageUI和iAd之类的框架。

So when I import these types of frameworks and then use their methods like this one for example: 因此,当我导入这些类型的框架,然后使用这种方法时,例如:

- (void)mailComposeController:(MFMailComposeViewController*)controller  
      didFinishWithResult:(MFMailComposeResult)result 
                    error:(NSError*)error;

Does that mean I am essentially using Polymorphism in Objective-C? 这是否意味着我实质上在Objective-C中使用了多态?

By definition the word Polymorphism means many forms. 根据定义,“多态”一词意味着多种形式。

Polymorphism in general is very broad topic and basically it invokes lot of things like method overloading, operator overloading, inheritance, reusability. 一般而言,多态是一个非常广泛的主题,基本上它会调用许多方法,例如方法重载,运算符重载,继承,可重用性。

And I don't think I implement polymorphism, rather I use the specific term like inheritance, operator overloading, method overloading etc 而且我不认为我实现了多态,而是使用了特定的术语,例如继承,运算符重载,方法重载等。

Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. Objective-C多态性意味着对成员函数的调用将导致执行不同的函数,具体取决于调用该函数的对象的类型。

For example - 例如 -

I have a base class Shape , defined as- 我有一个基类Shape ,定义为-

#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

I have derived two base classes Square and Rectangle from Shape as - 我从Shape派生了两个基类SquareRectangle作为-

@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

Now the calling method on any object will invoke the method of the corresponding class as- 现在,任何对象上的调用方法都将按以下方式调用相应类的方法:

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;
}

And as you asked about 就像你问的那样

- (void)mailComposeController:(MFMailComposeViewController*)controller  
      didFinishWithResult:(MFMailComposeResult)result 
      error:(NSError*)error;

the above method is the delegate method of MFMailComposeViewController class and yes as it is implemented by the delegate implementer, so it can have the customized implementation as per the requirements within the legal guidelines and so it's also a form of polymorphism(as the delegate method could be implemented in more than one way). 上面的方法是MFMailComposeViewController类的委托方法,是的,因为它是由委托实现者实现的,因此它可以根据法律准则中的要求进行自定义实现,因此它也是一种多态形式(因为委托方法可以以多种方式实施)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM