简体   繁体   English

如何在Objective-C中使用覆盖方法临时定义子类?

[英]How to temporarily define a subclass with overriding method in Objective-C?

I want to define a method in a class A. The method should be over-writable for any i̶n̶s̶t̶a̶n̶c̶e̶ subclass of the class A. The instances of subclasses will be stored in a NSArray. 我想在类A中定义一个方法。该方法对于类A的任何子类都应该是可重写的。子类的实例将存储在NSArray中。 I tried to use the delegate. 我试图使用委托。 But it didn't work. 但这没有用。 I checked a few articles posted online such as How do I create delegates in Objective-C? 我检查了一些在线发布的文章,例如如何在Objective-C中创建代表? and http://www.tutorialspoint.com/ios/ios_delegates.htm But they seem not what I can use to achieve my goal. http://www.tutorialspoint.com/ios/ios_delegates.htm,但它们似乎不是我可以用来实现目标的东西。 Could anyone who are familiar with this give me a hint please? 熟悉此事的人可以给我提示吗?

For example, 例如,

I create a base class called BaseClass for the UICollectionViewCell and inside the BaseClass there is a ActionMethod . 我为UICollectionViewCell创建了一个称为BaseClass的基类,并且在BaseClass内有一个ActionMethod Click different collection cell will result in a different action. 单击不同的收集单元将导致不同的操作。 So I will define a subclass for each cell that inherited from BaseClass to implement different action. 因此,我将为继承自BaseClass的每个单元格定义一个子类 ,以实现不同的操作。 Then how to overwrite the ActionMethod for each subclass? 那么如何覆盖每个子类的ActionMethod?

You mean overriding , not over-writing methods. 您的意思是覆盖 ,而不是覆盖方法。 In Objective-C, you don't have to do anything other than put an implementation with the exact same method signature in the subclass. 在Objective-C中,除了将具有完全相同的方法签名的实现放入子类中,您无需执行任何其他操作。

Generally, you define a base class, such as Shape, which has a set of methods common to all shapes, such as, getEnclosingRectangle, getShapeArea, etc. Then you define separate subclasses of shapes, such as oval, polygon, rectangle and triangle. 通常,您定义一个基类(例如Shape),该基类具有一组所有形状通用的方法,例如getEnclosingRectangle,getShapeArea等。然后定义形状的单独子类,例如椭圆形,多边形,矩形和三角形。 You can even define subclasses of those shapes, such as circle derived from oval. 您甚至可以定义这些形状的子类,例如从椭圆派生的圆形。 Then you could store all your ovals, rectangles and triangles (for example) in an NSArray, and call the getArea method for each of them. 然后,您可以将所有椭圆形,矩形和三角形(例如)存储在NSArray中,并为每个椭圆形调用getArea方法。 And each would have their particular getArea method defined in their class. 每个人都将在自己的类中定义其特定的getArea方法。

A delegate would work here. 一位代表将在这里工作。 Another option is to use a callback block, eg 另一种选择是使用回调块,例如

@interface Cell : NSObject

@property (nonatomic, copy) void(^onAction)();

- (void)actionMethod;

@end

@implementation Cell

- (void)actionMethod {
    self.onAction();
}

@end

Used as 用作

Cell *cell1 = [[Cell alloc] init];
cell1.onAction = ^{
    NSLog(@"Action 1");
};

Cell *cell2 = [[Cell alloc] init];
cell2.onAction = ^{
    NSLog(@"Action 2");
};

As you can see, you don't need subclasses, you can just add that code to every cell dynamically. 如您所见,您不需要子类,只需将代码动态添加到每个单元中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM