提问人:BPCorp 提问时间:11/29/2013 更新时间:11/29/2013 访问量:38
在“Class”方法中使用“MutableClass”方法
Use 'MutableClass' methods in 'Class' methods
问:
大家好
请考虑以下几点:
// Things.h
@interface Things : NSObject
@property (strong, nonatomic) NSSomething * listOfThings;
@end
// Things.m
@implementation Things
// Some methods
@end
// MutableThings.h
@interface MutableThings : Things
- (instancetype) thingsPassingTest;
@end
// MutableThings.m
@implementation MutableThings
- (instancetype)thingsPassingTest
{
// Do some stuff in order to return a subset of Things
return newThings;
}
@end
我希望能够调用一个对象,而不是一个.thingsPassingTest
Things
MutableThings
有没有办法调用子类方法?
或者也许我走错了方向?
答:
0赞
Greg
11/29/2013
#1
在您的设计中,MutableThings 继承自 Thinks,因此在 Things 中实现的每个公共属性和方法都将在 MutableThings 对象中可用。 你可以在 MutableThings 类中覆盖 thingsPassingTest 并返回 nil,也许这就是你想要的。 但最好的方法是使用 Things 和 MutableThings 类的所有常用方法创建根类。并且仅在 Things 中实现 thingsPassingTest,而不是在 MutableThings 中,也不在根类中。 这是你要求的东西吗?
评论
0赞
BPCorp
11/29/2013
我就是这么想的......我打算输入一个方法(因为它正在修改 MutableThings 实例)。现在,假设我需要调用才能创建一个 mutableThing,我需要把它放在类中吗?MutableThings
addThing
addThing
Things
0赞
Greg
11/29/2013
如果你也想在 Things 类中可用,就把它放在 Things 中。如果你只是在 MutableThings 中需要它,请不要将其添加到 Things 中。
0赞
Hashim Khan
11/29/2013
#2
不能对“Things”对象调用“thingsPassingTest”,因为“Things”是“MutableThings”的超类。它违背了面向对象编程范式的规则。
评论