Creating an abstract class in Objective-C

前端 未结 21 2656
感情败类
感情败类 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条回答
  •  -上瘾入骨i
    2020-11-22 16:22

    A simple example of creating an abstract class

    // Declare a protocol
    @protocol AbcProtocol 
    
    -(void)fnOne;
    -(void)fnTwo;
    
    @optional
    
    -(void)fnThree;
    
    @end
    
    // Abstract class
    @interface AbstractAbc : NSObject
    
    @end
    
    @implementation AbstractAbc
    
    -(id)init{
        self = [super init];
        if (self) {
        }
        return self;
    }
    
    -(void)fnOne{
    // Code
    }
    
    -(void)fnTwo{
    // Code
    }
    
    @end
    
    // Implementation class
    @interface ImpAbc : AbstractAbc
    
    @end
    
    @implementation ImpAbc
    
    -(id)init{
        self = [super init];
        if (self) {
        }
        return self;
    }
    
    // You may override it    
    -(void)fnOne{
    // Code
    }
    // You may override it
    -(void)fnTwo{
    // Code
    }
    
    -(void)fnThree{
    // Code
    }
    
    @end
    

提交回复
热议问题