Objective-C 没有抽象类,只是指语言的语法构造上没有C++/Java那样专门的abstract class定义,这并不妨碍我们在用Objective-C写程序的时候使用"abstract class"这一通用的OOP概念,即类的部分实现需要subclass来完成。
代码如下:
// 创建类Person 声明如下方法
@interface Person : NSObject
- (void)love;
- (void)coding;
- (void)travelling;
- (void)home;
@end
// 在.m文件中最好做如下处理
#define MethodNotImplemented() \
@throw \
[NSException exceptionWithName:NSInternalInconsistencyException \
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] \
userInfo:nil]
@implementation Person
- (instancetype)init {
self = [super init];
if (!self) return nil;
return self;
}
- (void)work { MethodNotImplemented(); }
- (void)coding {MethodNotImplemented(); }
- (void)love { MethodNotImplemented(); }
- (void)travelling { MethodNotImplemented(); }
- (void)home { MethodNotImplemented(); }
@end
这个声明的其实只是一个普通的基类,而且可以重写-init方法,那哪里抽象?和普通的类有什么区别?如果这个类需要有协议(代理),需要额外的属性,以及必须实现的的抽象方法,又该怎么处理?
下面,我又创建了一个Person+Private的类,这只是一个.h文件,并未实现任何的方法。
代码如下:
#import "Person.h"
@protocol PersonDelegate;
@interface Person ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, weak) id<PersonDelegate> delegate;
- (void)eat;
@end
@interface Person(Abstruct)
- (void)work;
@end
@protocol PersonDelegate <NSObject>
- (void)run;
@end
配合 MethodNotImplemented() 宏,在OC中的抽象基类就创建好了。在子类中调用对应的方法之前实现该抽象类的方法即可。
抽象类在第三方框架中的应用分析:
Masonry
在Masonry中的MASConstraint就是一个抽象类,上例中的Person也是遵照MASConstrain的写法写的一个示例。
在MASConstraint中作者这样写的:
@interface MASConstraint : NSObject
- (MASConstraint * (^)(MASEdgeInsets insets))insets;
......
- (MASConstraint *)left;
- (MASConstraint *)top;
- (MASConstraint *)right;
- (MASConstraint *)bottom;
- (MASConstraint *)leading;
- (MASConstraint *)trailing;
- (MASConstraint *)width;
- (MASConstraint *)height;
- (MASConstraint *)centerX;
- (MASConstraint *)centerY;
- (MASConstraint *)baseline;
- (MASConstraint *)firstBaseline;
- (MASConstraint *)lastBaseline;
@end
@implementation MASConstraint
- (MASConstraint * (^)(id))equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
- (MASConstraint * (^)(id))mas_equalTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
};
}
- (MASConstraint * (^)(id))greaterThanOrEqualTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);
};
}
- (MASConstraint * (^)(id))mas_greaterThanOrEqualTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);
};
}
- (MASConstraint * (^)(id))lessThanOrEqualTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);
};
}
- (MASConstraint * (^)(id))mas_lessThanOrEqualTo {
return ^id(id attribute) {
return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);
};
}
#pragma mark - MASLayoutPriority proxies
- (MASConstraint * (^)(void))priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}
- (MASConstraint * (^)(void))priorityMedium {
return ^id{
self.priority(MASLayoutPriorityDefaultMedium);
return self;
};
}
- (MASConstraint * (^)(void))priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
};
}
@end
MASConstraint+Private.h
@protocol MASConstraintDelegate;
// 延展类
@interface MASConstraint ()
@property (nonatomic, assign) BOOL updateExisting;
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;
- (void)setLayoutConstantWithValue:(NSValue *)value;
@end
@interface MASConstraint (Abstract)
- (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation;
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute;
@end
@protocol MASConstraintDelegate <NSObject>
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute;
@end
具体请参看Masonry源码。