Interface in C #
An Interface is defined as a contract with syntactical contract that all Interface inheritance classes should follow. Interface defines what part of the covenant and inheritance classes define which part of the covenant.
The interface in C # is defined as a syntactical contract that all Interface inheritance classes should follow. The interface defines the " What " section of the contract and the derived classes define the " How " section of the contract.
Interface defines attributes, methods and events, which are members of that Interface. The Interface contains only declarations of these members. Defining members is the responsibility of the derived class. It often helps provide a standard Structure that derived classes should follow.
The abstract classes to some extent usually serve the same purpose, however, they are mainly used when there are only a few methods declared by the base class and the derived class implements function.
Declaring Interface in C #
To declare the Interface in C # we use the interface
keyword. It is similar to class declaration. By default, the Interface command is public. This is the Interface declaration example in C #:
public interface GiaoDich { //các thành viên của interface void xemGiaoDich (); double laySoLuong (); }
For example:
The following example will illustrate the Interface implementation above:
using System . Collections . Generic ; using System . Linq ; using System . Text ; using System ; namespace GiaoDienUngDung { public interface QTM GiaoDich { //các thành viên của interface void xemGiaoDich (); double laySoLuong (); } public class GiaoDich : QTMGiaoDich { private string ma_giao_dich ; private string ngay_giao_dich ; private double so_luong ; //Lớp GiaoDich kế thừa từ QTMGiaoDich public GiaoDich () { ma_giao_dich = " " ; ngay_giao_dich = " " ; so_luong = 0.0 ; } public GiaoDich ( string c , string d , double a ) { ma_giao_dich = c ; ngay_giao_dich = d ; so_luong = a ; } public double laySoLuong () { return so_luong ; } public void xemGiaoDich () { Console . WriteLine ( "Giao dịch: {0}" , ma_giao_dich ); Console . WriteLine ( "Ngày giao dịch: {0}" , ngay_giao_dich ); Console . WriteLine ( "Số lượng: {0}" , laySoLuong ()); } } class Tester { static void Main ( string [] args ) { GiaoDich so 1 = new GiaoDich ( "001" , "08/06/2018" , 789000.00 ); GiaoDich so2 = new GiaoDich ( "002" , "09/06/2018" , 4519000.00 ); so1 . xemGiaoDich (); so2 . xemGiaoDich (); Console . ReadKey (); } } }
Running the above code we get the following result:
Transaction: 001
Transaction date: 08/06/2018
Quantity: 789000
Transaction: 002
Transaction date: 09/06/2018
Quantity: 4519000
According to Tutorialspoint
Previous article: Operator overloading in C #
Next lesson: Namespace in C #
You should read it
- Experience the Material Design 2 interface for Chrome, you can activate it
- 9 Best USB Audio Interface
- Return to Windows 7 interface from Windows 8
- Experience Google 's new Gmail interface quickly
- Messenger returns to the old interface after only a few hours of testing the new interface
- Microsoft has revealed the new Windows 10 interface, inviting you to view and comment
- Facebook will have a completely new interface for Vietnamese users
- Google will death the old Gmail interface, forcing users to use the new interface
- How to put the Inbox interface into Gmail
- How to simplify the Facebook interface on Chrome
- The basics of Cisco Switch Management - Part 1
- Turn every iPhone interface into iOS 6 without jailbreak