Interface 1 in C #. net

Source: Internet
Author: User
/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-8-16
* Time:
*
* Interface 1
*/

Using system;
Interface iadd
{
String strthing // the attributes and methods in the interface are public by default (if it is not public, it cannot be called by other classes, so it does not need to write keywords ).
{// If any keyword is written, an error is prompted. And cannot contain field members. You cannot use keywords static, virtual, abstract, or sealed.
Get;
Set;
}

Int add (int x, int y); // The method and attribute do not contain any code. The code is written in the class that implements the interface (the class that inherits the interface)
}
Interface iminus
{
String strname
{
Get;
}
Int minus (int x, int y );
}
Class interfacea: iadd, iminus // implementation code of the interface
{
Private string str1;
Public String strthing
{
Get {return str1 ;}
Set
{
Str1 = value;
}
}
Private string str2;
Public String strname
{
Get {return str2 ;}
Set
{
Str2 = value;
}
}
Public int add (int x, int y)
{
Return X + Y;
}
Public Virtual int minus (int x, int y) // you can use virtual or abstract to execute interface members, but not static or Const.
{
Return x-y;
}
}
Class interfaceb: interfacea
{
Public override int minus (int x, int y) // override method
{
Return 10 * x-y;
}
}
Class Test
{
Static void main ()
{
Iadd padd = new interfacea ();
Int intadd = padd. Add (); // The interface is actually used to classify functions in the class. Here, padd cannot call the minus method in the interface iminus.
Console. writeline (intadd); // 9 (4 + 5)

Iminus pminus1 = new interfacea ();
Int intminus1 = pminus1.minus (); // call the virtual method of the implementation interface in
Console. writeline (intminus1); //-1 (4-5)

Iminus pminus2 = new interfaceb ();
Int intminus2 = pminus2.minus (); // call the rewrite method in B.
Console. writeline (intminus2); // (10*4-5)
}
}

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.