Creating an abstract class in Objective-C

前端 未结 21 2707
感情败类
感情败类 2020-11-22 15:44

I\'m originally a Java programmer who now works with Objective-C. I\'d like to create an abstract class, but that doesn\'t appear to be possible in Objective-C. Is this poss

21条回答
  •  旧巷少年郎
    2020-11-22 16:12

    From the Omni Group mailing list:

    Objective-C doesn't have the abstract compiler construct like Java at this time.

    So all you do is define the abstract class as any other normal class and implement methods stubs for the abstract methods that either are empty or report non-support for selector. For example...

    - (id)someMethod:(SomeObject*)blah
    {
         [self doesNotRecognizeSelector:_cmd];
         return nil;
    }
    

    I also do the following to prevent the initialization of the abstract class via the default initializer.

    - (id)init
    {
         [self doesNotRecognizeSelector:_cmd];
         [self release];
         return nil;
    }
    

提交回复
热议问题