Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Protocols can conform to protocols

Protocols that declare themselves to conform to another protocol or protocols can be said to inherit from those protocols:

protocol TalkativePerson: Talkative 
{
... additional requirements
}

We might now begin to see what was meant by making subclassing unnecessary. There is no limit to the number of protocols to which a type, or another protocol, may conform. Similarly, there is no limit to the depth of inheritance; we could have ProtocolA, which inherits from ProtocolB, which in turn inherits from ProtocolC, and ProtocolD and so on.

This is a much more lightweight principle than subclassing, and also introduces the ability to mix and match protocols in a manner that many languages don't support in their class mechanism.

In the next section, we'll add some properties to our TalkativePerson protocol.