Interface [Reference]

Source: Internet
Author: User
This question is introduced from: [Author: loose_went] callback;

In some special cases (Code hiding, two interfaces implemented by a class have the same interface methods), you need to implement a specific interface method.

The interface definition can be zero or multiple members. The interface must be a method, attribute, event, or indexer. An interface cannot contain constants, fields, operators, instance constructors, destructor, or types, nor static members of any type.

The default access method for interface members is public. The interface member definition cannot contain any modifier. For example, the abstract, public, protected, internal, Private, virtual, override, or static modifier cannot be added before the member definition.

// ================================================ ======================== //

The Declaration interface syntax is exactly the same as the Declaration abstract class. For example, there is a bank account interface:
Public interface ibankaccount
{
Void payin (decimal amount );
Bool withdraw (decimal amount );

Decimal balance
{
Get;
}
}

Note: interfaces can only contain methods, attributes, indexers, and event declarations. It is not allowed to declare modifiers on Members, even pubilc, because interface members are always public and cannot be declared as virtual or static. If you need a modifier, it is best to let the implementation class declare it.

The interface of a bank account and the implementation classes of two different bank accounts are inherited from this interface. The interface declaration is as above. The following are two account classes:
Class SaverAccount: IBankAccount
{
Private decimal balance;

Public decimal Balance
{
Get
{
Return balance;
}
}

Public void PayIn (decimal amount)
{
Balance + = amount;
}

Public bool Withdraw (decimal amount)
{
If (balance> = amount)
{
Balance-= amount;
Return true;
}
Console. WriteLine ("Withdraw failed .");
Return false;
}

Public override string ToString ()
{
Return String. Format ("Venus Bank Saver: Balance = {0, 6: C}", balance );
}
}

Class GoldAccount: IBankAccount
{
Private decimal balance;

Public decimal Balance
{
Get
{
Return balance;
}
}

Public void PayIn (decimal amount)
{
Balance + = amount;
}

Public bool Withdraw (decimal amount)
{
If (balance> = amount)
{
Balance-= amount;
Return true;
}
Console. WriteLine ("Withdraw failed .");
Return false;
}

Public override string ToString ()
{
Return String. Format ("Jupiter Bank Saver: Balance = {0, 6: C}", balance );
}
}

It can be seen that these two implementation classes inherit the IBankAccount interface, so they must implement all declared methods in the interface. Otherwise, an error occurs during compilation. Let's test the code below:
Static void Main (string [] args)
{
IBankAccount venusAccount = new SaverAccount ();
IBankAccount jupiterAccount = new CurrentAccount ();
VenusAccount. Payment in (200 );
JupiterAccount. PayIn (500 );
Console. WriteLine (venusAccount. ToString ());
JupiterAccount. PayIn (400 );
JupiterAccount. Withdraw (500 );
JupiterAccount. Withdraw (100 );
Console. WriteLine (jupiterAccount. ToString ());
}
Pay attention to the first two sentences. We declare them as IBankAccount references instead of class references. Why? In this way, we can point it to any class instance that executes this interface, which is more flexible. However, this also has a disadvantage. If we want to execute methods that do not belong to the interface, such as the overloaded ToString () method, we need to forcibly convert the reference of the interface to a suitable type.

Interface inheritance

Interfaces can also inherit from each other, just like class inheritance. For example, we declare another interface ITransferBankAccount, which inherits from the IBankAccount interface.
Type.

Interface ITransferBankAccount: IBankAccount
{
Bool TransferTo (IBankAccount destination, decimal amount );
}
A new method TransferTo () is added to this interface. Therefore, if we want to write a class that inherits from ITransferBankAccount, we must implement all the method declarations for the IBankAccount and ITransferBankAccount interfaces. That is:
Class CurrentAccount: ITransferBankAccount
{
Private decimal balance;

Public decimal Balance
{
Get
{
Return balance;
}
}

Public void PayIn (decimal amount)
{
Balance + = amount;
}

Public bool Withdraw (decimal amount)
{
If (balance> = amount)
{
Balance-= amount;
Return true;
}
Console. WriteLine ("Withdraw failed .");
Return false;
}

Public override string ToString ()
{
Return string. Format ("Jupiter bank saver: balance = {0, 6: c}", balance );
}

Public bool transferto (ibankaccount destination, decimal amount)
{
If (withdraw (amount ))
{
Destination. payin (amount );
Return true;
}
Else
{
Return false;
}
}
}

To sum up, you should pay attention to the following issues when using the C # interface:

1. interfaces in C # are defined independently of classes. This is opposite to the C ++ model. In C ++, interfaces are actually abstract base classes.

2. interfaces and classes can inherit multiple interfaces.

3. A class can inherit a base class, and an interface cannot inherit a class at all. This model avoids the multi-Inheritance Problem of C ++, and implementations in different C ++ classes may conflict with each other. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. C #'s simplified interface model helps accelerate application development.

4. An interface defines a reference type with only abstract members. In C #, what an interface actually does is only the method flag, but no code is executed at all. This implies that one interface cannot be instantiated and only one object derived from this interface can be instantiated.

5. interfaces can define methods, attributes, and indexes. Therefore, to compare a class, the interface is special: when defining a class, it can be derived from multiple interfaces, and you can only derive from only one class.

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.