how to inherit from multiple class

前端 未结 5 757
自闭症患者
自闭症患者 2021-02-09 17:59

Let\'s say i have a griffon object that needs to be part of the felidae and bird class.
How do i do it ?
I can only make it inherit from 1 class at a time...

相关标签:
5条回答
  • 2021-02-09 18:24

    You can't, per se. But you can have references to as many other objects as you need and you can use multiple protocols.

    0 讨论(0)
  • 2021-02-09 18:25

    First, make felidae a subclass of bird. Piece of cake. :-)

    0 讨论(0)
  • 2021-02-09 18:27

    You can dynamically create a class at runtime and choose the methods of each parent class to inherit. Have a look at the NeXT runtime's documentation here about dynamically creating classes. I did this once just for fun, but I didn't get very far as it gets incredibly messy very quickly.

    Edit

    It gets more difficult though, because there can only be one superclass, otherwise the keyword super becomes ambiguous.

    0 讨论(0)
  • 2021-02-09 18:29

    This may help...

    Multiple inheritance

    • There is no innate multiple inheritance (of course some see this as a benefit). To get around it you can create a compound class, i.e. a class with instance variables that are ids of other objects. Instances can specifically redirect messages to any combination of the objects they are compounded of. (It isn't that much of a hassle and you have direct control over the inheritance logistics.) [Of course, this is not `getting around the problem of not having multiple inheritance', but just modeling your world slightly different in such a way that you don't need multiple inheritance.]

    • Protocols address the absence of multiple inheritance (MI) to some extent: Technically, protocols are equivalent to MI for purely "abstract" classes (see the answer on `Protocols' below).

    • [How does Delegation fit in here? Delegation is extending a class' functionality in a way anticipated by the designer of that class, without the need for subclassing. One can, of course, be the delegate of several objects of different classes. ]

    -Taken from http://burks.brighton.ac.uk/burks/language/objc/dekorte/0_old/intro.htm

    0 讨论(0)
  • 2021-02-09 18:35

    Multiple Inheritance in Objective C is not supported. The reason for not supporting this mechanism might be the fact that it would have been too difficult to include in the language or the authors thought it is a bad programming and design decision. However, in various cases multiple inheritance proves to be helpful. Fortunately objective C does provide some workarounds for achieving multiple inheritance. Following are the options:

    Option 1: Message Forwarding

    Message Forwarding, as the name suggests, is a mechanism offered by Objective C runtime. When a message is passed to an object and the object does not respond to it, the application crashes. But before crashing the objective c runtime provides a second chance for the program to pass the message to the proper object/class which actually responds to it. After tracing for the message till the top most superclass, the forwardInvocation message is called. By overriding this method, one can actually redirect the message to another class.

    Example: If there is a class named Car which has a property named carInfo which provides the car’s make, model and year of manufacture, and the carInfo contains the data in NSString format, it would be very helpful if NSString class methods could be called upon the objects of Car class which actually inherits from NSObject.

    - (id)forwardingTargetForSelector:(SEL)sel
    {
        if ([self.carInfo respondsToSelector:sel]) return self.carInfo;
        return nil;
    }
    

    Source: iOS 4 Developer's cookbook - Erica Sadun

    Option 2: Composition

    Composition is a cocoa design pattern which involves referencing another object and calling its functionalities whenever required. Composition actually is a technique for a view to build itself based on several other views. So, in Cocoa terminology this is very similar to Subclassing.

    @interface ClassA : NSObject {
    }
    
    -(void)methodA;
    
    @end
    
    @interface ClassB : NSObject {
    }
    
    -(void)methodB;
    
    @end
    
    @interface MyClass : NSObject {
      ClassA *a;
      ClassB *b;
    }
    
    -(id)initWithA:(ClassA *)anA b:(ClassB *)aB;
    
    -(void)methodA;
    -(void)methodB;
    
    @end
    

    Source: Objective-C multiple inheritance

    Option 3: Protocols

    Protocols are classes which contains method to be implemented by other classes who implement the protocol. One class can implement as many as protocols and can implement the methods. However, with protocols only methods can be inherited and not the instance variables.

    0 讨论(0)
提交回复
热议问题