Let's illustrate the definition and implementation of an interface with an example.
We use the bank account system to define and implement interfaces. The bank account system has many bank accounts, and they can deposit and withdraw accounts and check the balance between them. So we need to define an interface to identify the different operations of different accounts.
Start by defining an interface
public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } }
Naming interfaces in the C # domain needs to start with the capital letter I
Then define an account class, inherit the interface, and implement the content of the interface.
public class SaverCount:IBankAccount { private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount) { if (_balance>amount) { _balance -= amount; return true; } Console.WriteLine("Failure of withdrawal"); return false; } public decimal Balance => _balance; public override string ToString()=> $"Accounts come to:{_balance,6:C}"; }
- This class inherits the IBankAccount interface, but its base class is still System.Object. It should be noted that derivation from interfaces is completely independent of derivation from classes. That is to say, they can inherit and implement at the same time without affecting each other, but the grammatical inheritance base class must be placed in front of the interface.
- Inheritance of the interface means that all the members in the interface are acquired, but because the members in the interface are not implemented, all the members must be implemented in this class, otherwise the error will be reported.
- Classes of inherited interfaces can be abstract, which means that methods in implementing interfaces can also be abstract or virtual. For example, it is possible to change the PayIn of the above class to a virtual method.
public virtual void PayIn(decimal amount) => _balance += amount;
Different classes can implement the same interface. The following code example:
public class GoldAccount : IBankAccount { // ... public decimal Balance => throw new NotImplementedException(); public void PayIn(decimal amount) { throw new NotImplementedException(); } public bool Withdraw(decimal amount) { throw new NotImplementedException(); } }
Call.
static void Main(string[] args) { IBankAccount venusAccount = new SaverCount(); IBankAccount jupiterAccount = new GoldAccount(); venusAccount.PayIn(200); venusAccount.Withdraw(100); Console.WriteLine(venusAccount.ToString()); Console.WriteLine("---------"); jupiterAccount.PayIn(500); jupiterAccount.Withdraw(600); jupiterAccount.Withdraw(100); Console.WriteLine(jupiterAccount.ToString()); }
- As mentioned earlier, interfaces are not instanceable, but they can declare interface variables, so here we can declare an interface variable and instantiate the class we want to implement. This allows you to manipulate the current class through interface variables.
- If we don't create it through an interface, we won't achieve our goal (different banks operate the same account to synchronize money withdrawal and withdrawal)
- Interfaces can define data, and every element in an array is an instantiated class, but classes that do not implement interfaces cannot be assigned values.
IBankAccount[] venusAccount = new IBankAccount[2]; venusAccount[0] = new SaverCount(); venusAccount[1] = new GoldAccount();