- Mastering macOS Programming
- Stuart Grimshaw
- 136字
- 2021-07-02 22:54:37
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.