Objective-C有什么像C ++虚拟函数? [英] Does Objective-C have something like C++ virtual functions?

查看:163
本文介绍了Objective-C有什么像C ++虚拟函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于普通的实例方法,这也是可能的?



编辑
我想我还不够清楚。



我想做以下:

  @interface MyClass 
@property(retain)NSObject * somePropertyObject;
- (void)myMethod;
@end

@implementation MyClass

@dynamic somePropertyObject;

//使myMethod动态。我不想实现它。像C ++ Virtual

@end


解决方案

p>我想你可能会问如何声明一段时间以后实施的方法。



Objective-C的方法是使用 协议



您声明这样的协议,通常在头文件中

  @protocol MyProtocol< NSObject> ; {
@optional
- (void)optionalMethod;
@required
- (void)requiredMethod;
}
@end

这声明了两种方法,一种是可选的,一个是必需的。要使用此协议,在声明实现协议的类时声明一致性。

  @interface MyConformingClass:NSObject&MyProtocol> {
}
//您不必重新声明在协议中声明的方法
@end

这个新类在编译时检查执行 requiredMethod ,所以必须实现它,但它可以选择是否不实现 optionalMethod



现在,任何需要对象实例符合协议的类都可以声明这个例如,在界面中

  @interface RequiringClass:NSObject {
MyConformingClass&MyProtocol> * conformingClassObject;
}
...
@end

再次检查在编译时



为了确保符合类实现 @optional 方法,我们可以使用这个方便的结构

  if [conformingClassObject respondToSelector:@selector(optionalMethod)] {
[conformingClassObject optionalMethod];
} else {
//因为没有提供可选方法,在这里做某事
}

这个例子都是可可的 - 它是一个类可以提供一个列表,它希望将其纳入委托中,代理采用协议并提供这些委托方法的实现。调用对象可以检查这个代理是否像上面所描述的那样在运行时响应这些方法,并调用这些方法来执行操作,或提供需要的信息。


$ b $这在Objective-C中使用相当多,其中类提供了一些他们希望其他类执行的方法的列表,而不像虚拟函数那样,类声明了子类提供实现的函数。特别是作为组合在语言中继承的倾向。而不是创建一个子类来提供一个实现,你只需创建一个可以做同样事情的类,然后在类中添加一个引用。


In objective-c it is possible to add a @dynamic to a property.

Is this also possible for normal instance methods?

EDIT I think i wasn't clear enough.

I want to do the following:

@interface MyClass
@property (retain) NSObject *somePropertyObject;
- (void) myMethod;
@end

@implementation MyClass

@dynamic somePropertyObject;

//Make myMethod dynamic. I do not want to implement it. Like C++ Virtual

@end

解决方案

I think you might be asking how to declare a method that will be implemented some time later somewhere else.

The Objective-C way to do that is to use Protocols.

You declare a protocol like this, usually in a header file

@protocol MyProtocol <NSObject> {
@optional
    - (void)optionalMethod;
@required
    - (void)requiredMethod;
}
@end

This declares two methods, one which is optional and one is required. To use this protocol you declare the conformance when declaring the class that will implement the protocol

@interface MyConformingClass : NSObject <MyProtocol> {
}
// you don't have to redeclare methods that are declared in the protocol
@end

This new class is checked at compile time for the implementation of requiredMethod so it has to implement it, but it can choose whether or not to implement the optionalMethod

Now, any class that requires instances of objects to conform to the protocol can declare this, for example, in the interface

@interface RequiringClass : NSObject {
    MyConformingClass <MyProtocol> *conformingClassObject;
}
…
@end

Again, this is checked at compile time

To make sure that the conforming class implement the @optional methods, we can use this handy structure

if [conformingClassObject respondsToSelector:@selector(optionalMethod)] {
    [conformingClassObject optionalMethod];
} else {
    // Do something here because the optional method isn't provided
}

Examples of this are all over Cocoa - it's a class can provide a list of actions that it would like to farm out to it's delegate, the delegate adopts the protocol and provides the implementations of those delegate methods. The calling object can then check if this delegate responds to those methods at runtime as I've described above, and call those methods to perform actions, or provide information where ever it needs to.

This is used quite a lot in Objective-C, where classes provide a list of methods that they would like some other class to perform, unlike virtual functions, where a class declares functions it wants subclasses to provide implementations for. Particularly as Composition is favoured over inheritance in the language. Rather than create a subclass to provide an implementation, you just create another class that can do the same thing, and add a reference to that in the class instead.

这篇关于Objective-C有什么像C ++虚拟函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆