/*
* 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)
}
}