Previously, the concept of "interface" was vague. I read two articles in the garden today and started to understand the following:
Http://www.cnblogs.com/AndyFish/archive/2008/08/11/1265232.html (reproduced)
Http://www.cnblogs.com/anytao/archive/2007/04/12/must_net_02.html (reproduced)
Both authors are well summarized:
(1) similarities (abstract class and interface are used to define abstract classes in C #2.0 ):
They cannot be directly instantiated. They can all be inherited to implement their abstract methods;
It is the technical basis for abstract programming and implements many design modes;
(2) differences:
Abstract class can have its own data members or non-abstarct member methods. In an interface, only static data members cannot be modified. The interface does not allow any member implementation;
['Note this difference when designing the class']
Interfaces are used to find functional differences between objects with large differences. abstract base classes are used to find functional differences between objects with large differences.
-> This is why abstract class and interface have different design concepts.
-> It can be said that abstract class represents the "is-a" relation, and interface represents ("can-do relation") (or "like-a" relation );
This difference clearly represents the essential difference between abstract class and interface, which is described as follows:
Looking at the example given by the previous author, when writing a "alarm door" class, he uses:
Code class Door
{
Abstract void open ();
Abstract void close ();
}
Code class Car
{
Abstract void start ();
Abstract void park ();
}
Interface Alarm
{
Void alarm ();
}
Class AlarmDoor: Door, Alarm
{...}
Class AlarmCar: Car, Alarm
{...}
Explanation: AlarmDoor is essentially a Door (is door), but it can implement the "alarm" function (can do alarm)
AlarmCar is essentially a Car, but can be used to generate alarms (can do alarm)
The function of the interface is also reflected here: there are great differences between the "door", "car", and "alarm" functions, the use of the "interface" makes the two well integrated, although in C ++, class inheritance can also achieve Door-> AlarmDoor and Car-> AlarmCar, however, its design philosophy is not directly clear by using "interfaces.