Functions and importance of interface functionality & importance Interfaces

Source: Internet
Author: User
Interface is a new great type in CLR.

It's hard to represent just with words. We can describe the importance of interface using a sample as following shown.

In CLR, interface is a very important new type (c ++ does not have an interface, but abstract class)
It is difficult to use text instructions. I will give you an example and you will be clear :)

CASE 1: A sample without Interface
First case: no interface

Public sealed class chineseperson {...}

Public sealed class americanperson {...}

Public sealed class turnip {...} [turnip: radish, it's a mess]


Class doctor

{

Void operateandtransfuseblood (Object patient)

// What if patient is a turnip? Unable to verify!

{

}

}

In this sample, the doctor. operatonandtransfuseblood method accepts a single parameter of type system. object. the type system. object is the universal type in the CLR; this means that one canpass instances of any type as the parameter value, such as chineseperson instance, or americanperson instance, even a turnip instance. this logical error will not be discovered until runtime.
In this example, the doctor's blood test method requires a patient as the parameter, because there are two types of patients (Chinese and American ), to accept either of these two types, the common base class object is used as the parameter type. The problem may be that a radish can also be used as its parameter (sounds ridiculous) This error cannot be checked out in compile-time until run-time.
CAse2: a sample with interface to solve this problem.
Public interface ipatient {...}
Public sealed class chineseperson: ipatient {...}
Public sealed class americanperson: ipatient {...}
Public sealed class turnip {...}
Public class doctor
{
Void operationandtranfuseblood (ipatient patient)
{

In this example, there is a category of types called Ipatient , That category is declared as an interface. types that are compatible Ipatient Explicitly declare this compability as part of their type definition. both chineseperson and americanperson do exactly this. the operat .. blood method now declares its parameter to disallow types that are not comptible with ipatient. cos the turnip type did not declare compatibility with ipatient, attempts to pass turnip objects to this method will fail at compile time. this solutionis preferable to simply providing two explicit overloads of the method-one for chineseperson and one for ameracanperson -- cos this approach lets one define new types that one can pass to the doctor. operateand .. blood method without having to explicitly define new overloads.
In this example, we define an interface type ipatient. All compatible types are explicitly defined, that is, the inherited classes chineseperson and amercanperson. in this way, in the doctor. .. in blood, I use ipatient as the parameter type so that all ipatient-compatible parameters can be passed in. Because chineseperson and amercanperson both inherit from ipatient, the conditions can be met, turnip and ipatient are irrelevant. If the radish is used as its parameter, it will be checked in compile-time. This saves the overload operatio... blood method.

So cool!

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.