Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication1
{
/* The interface is declared, unimplemented, and cannot be instantiated;
The interface can contain methods, attributes, events, and indexers, but has no fields;
All interface members are implicitly public. Do not use access modifiers;
Classes, structures, and interfaces can all inherit multiple interfaces;
The class that inherits the interface must implement the interface member, unless it is an abstract class;
Class implementation interface members must be public and non-static .*/
// Define the interface
Interface Myinterface
{
// Declare a method in the interface without implementation or implementation
Int Num (int I, int s );
}
// Implementation Interface
Class MyClass: Myinterface
{
Public int Num (int I, int s) // because all the members in the interface are public, public is also implemented. Otherwise, an error is returned.
{Return I + s ;}
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////
// Try multiple implementations of one interface below.
Interface Myinterface1
{
Int Num (int I, int s );
}
// Multiple implementations.
Class Add: Myinterface1 // implement 1 Addition
{
Public int Num (int I, int s)
{Return I + s ;}
}
// Implements 2 Subtraction
Class Sub: Myinterface1
{
Public int Num (int I, int s)
{Return I-s ;}
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////
// Multiple interfaces are implemented in one class.
Interface face
{Int Num (int I, int s );}
Interface face1
{Int Num (int I, int s );}
// A class to implement two interfaces and display the implementation.
/* Access modifiers are not required for display implementation interfaces, but display implementation methods can only be accessed through interfaces */
Class MyFace: face, face1
{Int face. Num (int I, int s) {return I + s;} // implement the first one.
Int face1.Num (int I, int s) {return I-s;} // implements the second one.
}
Class Program
{
Static void Main (string [] args)
{
// Single implementation of a single interface
MyClass My = new MyClass ();
Console. WriteLine (My. Num (10, 9); // 19
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////
// Multiple implementations of one interface.
Add jiafa = new Add ();
Console. WriteLine (jiafa. Num (56, 35); // 91
Sub jianfa = new Sub ();
Console. WriteLine (jianfa. Num (56, 35); // 21
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////
// Multiple interfaces are implemented in one class. They can only be accessed through interfaces.
Face Myjiafa = new MyFace ();
Face1 Myjianfa = new MyFace ();
Console. WriteLine (Myjiafa. Num (20, 10); // 30
Console. WriteLine (Myjianfa. Num (20, 10); // 10
}
}
}