在Objective-C的理解@Protocols

问题描述:

我是一个初学者到编程和初学者的Objective-C。我学会了基本的C和决定开始学习Objective-C。我读编程在Objective C 2.0由史蒂芬科昌。
他对协议部分是模糊的。他没有详细解释为什么会有人想要使用的协议在他们的计划,他也没有给出一个具体的例子用一个程序实现。
他写:
您可以使用一个协议来定义你想要谁继承你的类来实现其他人的方法。
他还表示,协议是很好的子类,能够实现一定的方法,而不必首先定义实际的方法。他还表示,协议可以在不同的类中使用,因为它们是无阶级的。

I am a beginner to programming, and a beginner to Objective-C. I learned basic C and decided to start learning Objective-C. I am reading "Programming in Objective C 2.0" by Steven Kochan. His section on Protocols is vague. He doesn't thoroughly explain WHY someone would want to use protocols in their programs, nor does he give a concrete example with it implemented in a program. He writes: "You can use a protocol to define methods that you want other people who subclass your class to implement." He also says that Protocols are good for sub-classes to be able to implement certain methods, without having to first define the actual methods. He also says protocols can be used across different classes because they are classless.

我知道必须有执行协议的有效和智能的方式,而是基于他写的东西,我不明白为什么会有人使用协议,而不是只是一个以上的类的原因之外创建一个类的方法能够坚持协议(我知道有一些很好的理由,虽然!)。
我想知道,如果有人可以帮助我了解:
如何,为什么,当我想以智能方式使用协议我的程序。

I know there must be a valid and smart way to implement protocols, but based on what he wrote, I don't see why someone would use protocols instead of just creating a class method outside of the reason that more than one class can adhere to a protocol (I know there are some more good reasons though!). I was wondering if someone could help me understand: how, why and when I would use Protocols in my program in an intelligent way.

如果你做任何类型的面向对象编程,你可能知道的协议为接口(他们是不完全相同,但概念是相似的)。如果不是这样,认为协议的蓝图。

If you've done any kind of object-oriented programming, you probably know protocols as interfaces (they're not identical, but the concept is similar). If not, think of protocols as blueprints.

为什么要使用协议的主要原因是,所以你可以使用对象不知道他们的一切;所有你需要知道的是,他们实施了一套方法。例如,如果类商业遵守协议联系,它定义了方法 - (的NSString *)phoneNumber的,类地址簿可拨打 - (的NSString *)phoneNumber的不知道该对象是否是类型商业

The main reason why you'd use protocols is so you can use objects without knowing everything about them; all you need to know is that they implement a set of methods. For example, if the classes Business and Person conform to the protocol Contact, which defines the method - (NSString *)phoneNumber, the class AddressBook can call -(NSString *)phoneNumber without knowing whether or not the object is of type Business or Person.

一旦你开始了解可可和代表,你会看到重要的协议是多么强大,有。

Once you start to learn about Cocoa and delegates, you'll see how powerful and important protocols are.