C # interface Learning

Source: Internet
Author: User

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

}
}
}

Related Article

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.