In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to declare and implement the C# interface". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to declare and implement the C# interface.
Learn the C# interface: declare the interface
The declaration interface is syntactically the same 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 for methods, properties, indexers, and events. Modifiers on members are not allowed to be declared, not even pubilc, because interface members are always public and cannot be declared virtual and static. If a modifier is needed, * let the implementation class declare it.
Learning the C# interface: an example of using the C# interface
This is a simple example in the book, but it is sufficient to illustrate the use of the interface.
The interface of one bank account and the implementation classes of two different bank accounts all inherit 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= {0je 6rig 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= {0je 6return false; C}", balance);}}
As you can see, most of these 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; jupiterAccount.PayIn; Console.WriteLine (venusAccount.ToString ()); jupiterAccount.PayIn; jupiterAccount.Withdraw (500); jupiterAccount.Withdraw; Console.WriteLine (jupiterAccount.ToString ());}
Notice the first two sentences, the way we declare them as IBankAccount references, not as references to the class. Why? Because, in this way, we can make it point to an instance of any class that executes this interface, which is more flexible. But there is a drawback: if we want to execute a method that is not part of an interface, such as the overloaded ToString () method here, we have to cast a reference to the interface to the appropriate type.
Inheritance of interface
Interfaces can also inherit from each other, just like class inheritance. For example, we declare another 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 want to write a class that inherits from ITransferBankAccount, we must implement all the method declarations of 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= {0je 6rig C}", balance) } public bool TransferTo (IBankAccount destination, decimal amount) {if (Withdraw (amount)) {destination.PayIn (amount); return true;} else {return false;}
There are several problems you should pay attention to when using the learning C# interface:
1. The C# interface is defined independently of the class. This is opposed to the C++ model, where the interface is actually an abstract base class.
2. Both 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 problem of multi-inheritance of C++, and the implementations in different base classes in C++ may conflict. As a result, complex mechanisms such as virtual inheritance and explicit scope are no longer needed. The simplified interface model of C # helps to speed up the development of applications.
4. An interface defines a reference type with only abstract members. What an interface in C # actually does is only the method flag, but there is no code execution at all. This implies that an interface cannot be instantiated, only one object derived from that interface can be instantiated.
5. Interfaces can define methods, properties, and indexes. So, compared to a class, the particularity of an interface is that when a class is defined, it can be derived from multiple interfaces, while you can only derive from only one class.
Thank you for your reading. the above is the content of "how to declare and implement the C# interface". After the study of this article, I believe you have a deeper understanding of how to declare and implement the C# interface. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.