Objective-c polymorphism

Source: Internet
Author: User





In object-oriented languages, many different implementations of interfaces are polymorphic. Polymorphism responds to different objects in response to the same method, polymorphism in code, that is, a variety of forms, must have inheritance, no inheritance there is no polymorphism in the use of polymorphism, will be dynamic detection, to invoke the real object method
//
// main.m
// mobile

#import <Foundation / Foundation.h>
#import "Communication.h"
#import "Electrograph.h"
#import "Telephone.h"
#import "Student.h"
int main (int argc, const char * argv []) {
    @autoreleasepool {
        Peerson * p = [[Student alloc] init];
      Communication * cuo = [[Electrograph alloc] init];
        [p sayhi: cuo];
        cuo = [[Communication alloc] init];
        [p sayhi: cuo];
        cuo = [[Telephone alloc] init];
        [p sayhi: cuo];
    }
    return 0;
}
 

//
// Peerson.h
// mobile
#import <Foundation / Foundation.h>
#import "Communication.h"
@interface Peerson: NSObject
-(void) sayhi: (Communication *) say;
@end
//
// Peerson.m
// mobile

#import "Peerson.h"

@implementation Peerson
-(void) sayhi: (Communication *) say {
    [say call];
}
@end
//
// Student.h
// mobile
#import "Peerson.h"

@interface Student: Peerson

@end

//
// Student.h
// mobile


#import "Peerson.h"

@interface Student: Peerson

@end
//
// Communication.h
// mobile

#import <Foundation / Foundation.h>

@interface Communication: NSObject
-(void) call;
@end

//
// Communication.m
// mobile

#import "Communication.h"

@implementation Communication
-(void) call {
    NSLog (@ "I'm calling now");
}
@end
//
// Electrograph.h
// mobile

#import "Communication.h"

@interface Electrograph: Communication

@end

//
// Electrograph.m
// mobile
#import "Electrograph.h"

@implementation Electrograph
-(void) call {
    NSLog (@ "I'm calling with my family");
}
@end
//
// Telephone.h
// mobile
#import "Communication.h"

@interface Telephone: Communication

@end

//
// Telephone.m
// mobile

#import "Telephone.h"

@implementation Telephone
-(void) call {
    NSLog (@ "My mobile phone can access the Internet");
}
@end

Objective-c polymorphism


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.