Description of what an Interface does? [duplicate]

2019-04-11 11:00发布

Duplicate: Interfaces: Why can't I seem to grasp them?

With regards to OOP,how would you describe an interface?

What i mean is , sub-classing can be described as "Has-A" and Inheritance could be "Is-A". A member method could be "Can-Do" ..

Is there any way this could be extended (no pun intended) to describe what an Interface does?

Thanks

标签: oop interface
5条回答
对你真心纯属浪费
2楼-- · 2019-04-11 11:34

Acts-As-A.

查看更多
Ridiculous、
3楼-- · 2019-04-11 11:37

Joel that isn't exaclty what an interface is. It is like a abstract base class in a way but it has no implementation of the methods and properties.

This pretty much sums up what an interface is.

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_interrfaces03052006095933AM/csharp_interrfaces.aspx?ArticleID=cd6a6952-530a-4250-a6d7-54717ef3b345

查看更多
老娘就宠你
4楼-- · 2019-04-11 11:39

An interface is an abstract base class with all pure virtual members.

So looking at your Has-A/Is-A, it should be similar to whatever you would be apply for an abstract base class.

Interfaces generally exist in languages that do not fully support multiple inheritance as a way to more safely offer some of the same benefits.

查看更多
叼着烟拽天下
5楼-- · 2019-04-11 11:40

As is your description for methods, I would also describe an interface as a "Can-Do". An interface is a contract like "all the classes that implement me, can do these things".

查看更多
小情绪 Triste *
6楼-- · 2019-04-11 11:44

I think of objects as nouns, methods as verbs, and interfaces as adjectives (of course this analogy is oversimplified, but frequently works well enough).

Example: an interface Serializable works like an adjective, in that it applies some qualities to an object that implement that interface, but does not change what that object is. We can say, "this is a serializable object." But we don't say, "this object is a serializable," nor do we say, "this object has a serializable."

I also like Federico's answer that an interface is "CAN-DO".

An interface is a group of related operations that the class supports. Together, the methods in an interface describe what the class can do.

Just like a noun can take multiple adjectives, a class can implement multiple interfaces, as long as they don't conflict. The union of all the interfaces a class implements is the sum of what the class can do.

In practical terms, an interface is a set of method signatures, without the code implementing those methods. Just the method name and arguments. Depending on the language, a method signature may also include return type, and exceptions thrown.

An interface consists of methods, but not data members.

BTW, I wouldn't say sub-classing is HAS-A. My understanding is that sub-classing is the same as inheritance, so these are both IS-A. Whereas HAS-A is called Aggregation or Composition.

  • Composition is where an object owns another object. Destroying the outer object also destroys the inner objects. Example: University composes Departments. Close the University, and the Departments disappear.

  • Aggregation is where an object includes another object, but does not own it. Destroying the outer object does not destroy the inner objects. Example: University employs Professors, but closing the University does not kill the Professors.

查看更多
登录 后发表回答