What is an interface? In fact, the interface is a simple understanding is a convention, so that the implementation of the interface class or structure in the form of consistency. Personally, the use of interfaces can make programs clearer and more organized, which is the benefits of interfaces, but not all programming languages support interfaces, C # is to support the interface. Note that although the C # interface is conceptually similar to a COM interface, their underlying structure is different. So, let's look at how to declare and use interfaces.
declaring interfaces
Declaring an interface is exactly the same syntax as declaring an abstract class, for example, there is an interface for a bank account:
public interface Ibankaccount
{
void Payin (decimal amount);
BOOL Withdraw (decimal amount);
Decimal Balance
{
Get
}
}
Note: Interfaces can only contain declarations of methods, properties, indexers, and events. It is not allowed to declare modifiers on members, even PUBILC, because interface members are always public and cannot be declared as virtual and static. If a modifier is required, it is best to have the implementation class declare it.
Examples of using interfaces
This is a simple example of a book, but it is sufficient to illustrate how the interface is used.
The interface of a bank account, the implementation classes of two different bank accounts, are inherited from this interface. The interface declaration is as above. Here 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);
}
}
As can be seen, the two implementation classes inherit the Ibankaccount interface, so they must implement all the declared methods in the interface. Otherwise, the compilation will go wrong. Let's test it, here's the test code:
static void Main (string[] args)
{
Ibankaccount venusaccount = new Saveraccount ();
Ibankaccount jupiteraccount = new Currentaccount ();
Venusaccount.payin (200);
Jupiteraccount.payin (500);
Console.WriteLine (Venusaccount.tostring ());
Jupiteraccount.payin (400);
Jupiteraccount.withdraw (500);
Jupiteraccount.withdraw (100);
Console.WriteLine (Jupiteraccount.tostring ());
}
Note that the first two sentences, we declare them as ibankaccount references, and not as references to classes, why? Because, so we can point it to the execution of any class instance of this interface, more flexible. But this also has a drawback, if we want to execute methods that are not part of the interface, such as the overloaded ToString () method here, the interface must first cast the reference to the appropriate type.
Inheritance of interfaces
Interfaces can also inherit from each other, just as the class inherits. For example, we declare an interface Itransferbankaccount, which inherits from the Ibankaccount interface.
Interface Itransferbankaccount:ibankaccount
{
BOOL TransferTo (ibankaccount destination, decimal amount);
}
In this interface, a new method Transferto () is added, so if we are going to write a class that inherits from Itransferbankaccount, It is necessary to implement all the method declarations of the Ibankaccount and Itransferbankaccount two interfaces. That
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;
}
}
}
In summary, there are several issues to be aware of when using C # interfaces:
1. The interfaces in C # are defined independently of the class. This is in contrast to the C + + model, where interfaces are actually abstract base classes in C + +.
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 multiple inheritance problem of C + +, and the implementation of different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. The simplified interface model of C # helps accelerate application development.
4. An interface defines a reference type that has only abstract members. What an interface in C # actually does, there is only a method flag, but there is no execution code at all. This implies that an interface cannot be instantiated, and only one object derived from that interface can be instantiated.
5. Interfaces can define methods, properties, and indexes. So, in contrast to a class, the particularity of an interface is that when a class is defined, it can be derived from a multiple interface, and you can only derive from one of the only classes.
C # interface