. NET Beginner Interface First Contact

Source: Internet
Author: User

1. Interface definition interface is for unified standard, keyword interface. When defining an interface, write the letter I, such as interface, before the interface name.Ijiekou{string Fangfa ();//method without method body} 2.    (1) The method inside the interface has no method body, no modifier, no implementation.    (2) The interface also has the transitivity, cannot be instantiated, cannot write the method with the method body.    (3) The ordinary class inherits the interface must implement its non-implementation method, the interface inheritance interface may not implement its method, the abstract class cannot inherit the interface. (4) A common class can inherit multiple interfaces. 3. Similarities and differences between interfaces and abstract classes
Abstract class Interface
Vary Define with abstract Define with interface
Only one class can be inherited can inherit multiple interfaces
Non-abstract derived classes must implement abstract methods Classes that inherit an interface must implement all methods
The Override keyword is required Direct implementation
A method that can write a method body Can't write methods that have a method body
With 1. Neither can be instantiated
2. Include methods that are not implemented
3. Derived classes must implement methods that are not implemented

4. The interface is passed as a parameter, the object that implements the interface is passed, the interface is returned as the return type, and the object that implements the interface is returned.
1  Public classA2 {3      InterfaceIjiekou ()//Define Interface Ijiekou4   {5       intNum ();6   }7 }8  9  Public classB:ijiekou//Class B Inheritance Interface IjiekouTen { One    Public inta A   { -        Public intNum () -     { the         return 1; -     } -}

1  Static voidMain (string[] args)2   {3Ijiekou J =NewB ();//interface Instantiation Class B4a.x =NewA ();//Instantiating Classes5X.chuancan (J);//to pass J into the Chuancan class.6   }7    Public intChuancan (Ijiekou i)//here is the use of the interface as a parameter8   {9Console.WriteLine (I.num)//The return value of the A method in the output Class BTen}
1 Static voidMain (string[] args)2   {3Ijiekou J =NewB ();4 Console.WriteLine (j.fanhui.num);5   }6    PublicIjiekou Fanhui ()//here is the interface as the return type7   {8Ijiekou JJ =NewB ();9      returnJJ;Ten   } One}

. NET Beginner Interface First Contact

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.