Interface định nghĩa các thuộc tính, phương thức và sự kiện - là các thành viên của interface. Interface chỉ chứa khai báo, lớp triển khai chịu trách nhiệm định nghĩa chi tiết. Giúp cung cấp cấu trúc chuẩn cho các lớp dẫn xuất.
Cú pháp khai báo interface:
public interface InterfaceName {
// các thành viên interface
}
Ví dụ
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace Demo {
public interface ITransactions {
void showTransaction();
}
public class Transaction : ITransactions {
private string tCode;
private string date;
public Transaction() {
tCode = "";
date = "";
}
public Transaction(string c, string d) {
tCode = c;
date = d;
}
public void showTransaction() {
Console.WriteLine("Transaction ID: {0}", tCode);
Console.WriteLine("Date: {0}", date);
}
}
class Tester {
static void Main(string[] args) {
Transaction t1 = new Transaction("8877", "6/25/2018");
Transaction t2 = new Transaction("5656", "7/25/2018");
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}