問題描述
繼承是一種“is-a”關係;總結“界面”關係的好方法是什麼?——總結其他關係? (inheritance is an 'is-a' relationship; What is a good way to summarize the 'interface' relationship? -- Summarizing other relationships?)
Title: inheritance is an 'is-a' relationship; What is a good way to summarize the 'interface' relationship?
Also, I think it would be nice to have a repository of simple relationships to help people put things in perspective. I'm fairly new to programming so I'm not sure if there's really enough 'relationships' to warrant this aspiration-- but if there is, this may be a good place to start.
- inheritance = "is-a"
- composition = "has-a"
- interface implementation = ?...
參考解法
方法 1:
Interface implementation = "can-do"
An interface usually represents one ability that you can implement in a class, for example in .NET the IEnumerable
interface is used for classes that you can enumerate, and IComparable
for classes that you can compare.
方法 2:
why not just "implements a"? guess I'm not sure what you're driving at.
方法 3:
Your predicates is-a and has-a describe the structural relation between different types. In that sense, an interface should also get the is-a.
However, there are more possible taxonomies and also more oop key concepts:
- An interface is a contract that each implementor fulfils (promises-to).
- Connection between objects can be established not only using composition but also by passing objects to methods (uses-a) or involving kind of messaging (notifies-of).
- The purpose of inheritance is not only polymorphy (as-a) by differently implementing the base class methods but also to add new functionality (properties, methods) - (extends-a).
- The role of objects in an OOP application may be classified with manages-a, creates-a, visits-a, substitutes-a, etc.
- ...
方法 4:
Maybe "as-a" or "like-a": Implying that it has some properties/abilities but isn't directly derived from.
方法 5:
Interface is a "fits-into" relationship.
Think about the electrical socket in your wall. That is your interface to the electrical grid. You may have many objects that share such an interface (i.e. they all "fit-into" the socket).
Other countries have different interfaces, along with differenct sets of objects which will "fit-into" those particular schemes.
(by Evan Sevy、Guffa、jcomeau_ictx、Kaken Bok、Jon Egerton、Jimmy)