Can the Object-c class inherit multiple? Can I implement multiple interfaces? How is it implemented?

Source: Internet
Author: User

Can the Object-c class inherit multiple? Can I implement multiple interfaces?

The class of object-c cannot inherit multiple, and multiple interfaces can be implemented to accomplish multiple inheritance of C + +, although OC is syntactically forbidden to use multiple inheritance for classes, but it allows multiple inheritance to be used in protocol compliance. So you can use protocols to achieve multiple inheritance. But the protocol can only provide the interface, and not provide the implementation way, if only want to inherit the base class interface, then abide by the multi-protocol is undoubtedly the best method, and not only need to inherit the interface, but also inherit its implementation, then the protocol is powerless. Multi-Protocol compliance is relatively simple, concrete implementation of the way here is not talking!

Now CLASSC need to inherit ClassA in MethodA, ClassB MethodB, the specific code implementation is:

Define ClassA and its MethodA

@interface Classa:nsobject {
}

-(void) MethodA;

@end
Define CLASSB and its METHODB
@interface Classb:nsobject {
}

-(void) MethodB;

@end
Define the CLASSC and the methoda,methodb it needs
@interface Classc:nsobject {
ClassA *a;
ClassB *b;
}

-(ID) Initwitha: (ClassA *) A B: (ClassB *) b;

-(void) MethodA;
-(void) MethodB;

@end

Note the implementation of the CLASSC

@implementation ClassC

-(ID) Initwitha: (ClassA *) A B: (ClassB *) b{

A=[[classa alloc] initwithclassa:a];//[a copy];

B=[[CLASSB alloc] initwithclassb:b];//[b copy];

}

-(void) methoda{

[A MethodA];

}
-(void) methodb{

[B MethodB];

}

Can the Object-c class inherit multiple? Can I implement multiple interfaces? How is it implemented?

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.