C #: Interface)

Source: Internet
Author: User

Http://www.cnblogs.com/LilianChen/archive/2013/03/05/2944627.html

  1. The interface name starts with the letter I to know that this is an interface
  2. Derived from the interface is completely independent from the derived from the class
  3. When declaring an interface member, you only need to specify the name and parameters of the interface member,Once an interface is inherited, The subclass must provide all the implementation code of the method.
  4. The interface declaration does not include data members. It can only contain methods, attributes, events, indexes, and other Members. 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.
  5. Both interfaces and classes can inherit multiple interfaces.
    Using system; namespace consoleapplication3 {class program {static void main (string [] ARGs) {/* Here we declare them as the reference method of ibankaccount, without being declared as a reference to the class. This means they can point to any class instance that implements this interface. However, the disadvantage is that you can only call methods in the interface through these references. If you want to call methods that are executed by the class and are not in the interface (for example, the tostring () method overwritten here ), you need to forcibly convert the reference to a suitable type. However, there is no display conversion here, because tostring () is a system. object method. C # The Compiler knows that any class supports this method. implicit object data type conversion) */ibankaccount abcacount = new saveraccount (); abcacount. payin (200); abcacount. withdraw (100); console. writeline (abcacount. tostring (); ibankaccount icbcacount = new goldaccount (); icbcacount. payin (1, 500); icbcacount. withdraw (600); icbcacount. withdraw (100); console. writeline (icbcacount. tostring (); conso Le. readline () ;}/// the Declaration interface syntax is exactly the same as the Declaration abstract class public interface ibankaccount {// The access level of the interface member is the default (the default is public ), therefore, you cannot specify any access modifiers for interface members when declaring them. // otherwise, the compiler reports an error. Void payin (decimal amount); bool withdraw (decimal amount); decimal balance {Get ;}} public class saveraccount: 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 ("withdraw attempt failed"); Return false;} public decimal balance {get {return balance ;}} public override string tostring () {return string. format ("ABC Bank saver: balance = {0, 6: c}", balance) ;}} public class goldaccount: 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 ("withdraw attempt failed"); Return false;} public decimal balance {get {return balance ;}} public override string tostring () {return string. format ("ICBC bank2 saver: balance = {0, 6: c}", balance );}}}

     

  6. An interface can reference any class implemented to an interface. For example, an interface array can be constructed, and each element of an interface is a different class:

1             IBankAccount[] accounts = new IBankAccount[2];2             accounts[0] = new SaverAccount();3             accounts[1] = new GoldAccount();

 

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.