I read two articles in the garden and initially understood the meaning of "interface ".

Source: Internet
Author: User
Tags define abstract

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.