目标C - 实施和扩展之间的差异

时间:2022-09-10 21:28:02

Just a general question regarding the difference between extending a class and implementing it.

关于扩展类和实现它之间的区别的一般问题。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

In my example my class AppDelegate extends from the UIResponder Class but implements the UIApplicationDelegate class.

在我的示例中,我的类AppDelegate从UIResponder类扩展,但实现了UIApplicationDelegate类。

Can somebody please explain... Do you have also possibilities to access methods of the UIApplicationDelegate object/ class? Where are the differences?

有人可以解释一下......您是否也有可能访问UIApplicationDelegate对象/类的方法?差异在哪里?

Thanks

谢谢

3 个解决方案

#1


1  

If you are speaking in Java terms, then in your example you are Extending (generally referred to as Subclassing) the UIResponder class.

如果您使用Java术语,那么在您的示例中,您将扩展(通常称为Subclassing)UIResponder类。

Your example is also implementing the UIApplicationDelegate which is similar to implementing an Interface where you have to define the required methods of the UIApplicationDelegate Protocol and you have the option of implementing the optional methods.

您的示例还实现了UIApplicationDelegate,它类似于实现一个接口,您必须定义UIApplicationDelegate协议所需的方法,并且您可以选择实现可选方法。

Similar to Java, you can only extend or subclass one class but you can implement multiple delegate protocols in your class

与Java类似,您只能扩展或子类化一个类,但您可以在类中实现多个委托协议

Apple's guide to Protocols is very handy if you want to understand more: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html

如果您想了解更多信息,Apple的协议指南非常方便:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html

#2


0  

At very basic level we can see this a form to achieve multiple inheritence in objective C.

在非常基本的层面上,我们可以看到这种形式在目标C中实现多重继承。

As only single inheritence is supported by objective C, but we can achieve multiple iinheritence by using protocols(Which is similar to interfaces in java).

由于目标C只支持单个继承,但我们可以通过使用协议(类似于java中的接口)来实现多个iinheritence。

So When we subclass a class - then we say we extend that.

所以当我们继承一个类时 - 那么我们说我们扩展它。

and when we use protocol to access common functionality(in order to have multiple inheritende) - we say it implements.

当我们使用协议来访问常用功能时(为了拥有多个继承) - 我们说它实现了。

#3


0  

Extending a class = subclassing it. I.e. the methods you don't override will be called in the superclass.

扩展一个class =继承它。即您不会覆盖的方法将在超类中调用。

Implementing a delegate protocol simply says what methods your class is supposed to have implemented.

实现委托协议只是说明了您的类应该实现的方法。

See (for example) https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1

请参阅(例如)https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1

and

http://www.thepensiveprogrammer.com/2010/05/objective-c-protocols-and-delegates.html

http://www.thepensiveprogrammer.com/2010/05/objective-c-protocols-and-delegates.html

#1


1  

If you are speaking in Java terms, then in your example you are Extending (generally referred to as Subclassing) the UIResponder class.

如果您使用Java术语,那么在您的示例中,您将扩展(通常称为Subclassing)UIResponder类。

Your example is also implementing the UIApplicationDelegate which is similar to implementing an Interface where you have to define the required methods of the UIApplicationDelegate Protocol and you have the option of implementing the optional methods.

您的示例还实现了UIApplicationDelegate,它类似于实现一个接口,您必须定义UIApplicationDelegate协议所需的方法,并且您可以选择实现可选方法。

Similar to Java, you can only extend or subclass one class but you can implement multiple delegate protocols in your class

与Java类似,您只能扩展或子类化一个类,但您可以在类中实现多个委托协议

Apple's guide to Protocols is very handy if you want to understand more: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html

如果您想了解更多信息,Apple的协议指南非常方便:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html

#2


0  

At very basic level we can see this a form to achieve multiple inheritence in objective C.

在非常基本的层面上,我们可以看到这种形式在目标C中实现多重继承。

As only single inheritence is supported by objective C, but we can achieve multiple iinheritence by using protocols(Which is similar to interfaces in java).

由于目标C只支持单个继承,但我们可以通过使用协议(类似于java中的接口)来实现多个iinheritence。

So When we subclass a class - then we say we extend that.

所以当我们继承一个类时 - 那么我们说我们扩展它。

and when we use protocol to access common functionality(in order to have multiple inheritende) - we say it implements.

当我们使用协议来访问常用功能时(为了拥有多个继承) - 我们说它实现了。

#3


0  

Extending a class = subclassing it. I.e. the methods you don't override will be called in the superclass.

扩展一个class =继承它。即您不会覆盖的方法将在超类中调用。

Implementing a delegate protocol simply says what methods your class is supposed to have implemented.

实现委托协议只是说明了您的类应该实现的方法。

See (for example) https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1

请参阅(例如)https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1

and

http://www.thepensiveprogrammer.com/2010/05/objective-c-protocols-and-delegates.html

http://www.thepensiveprogrammer.com/2010/05/objective-c-protocols-and-delegates.html