Interface trong C | 85 bài học lập trình C hay nhất PDF

3 103 0
Interface trong C  | 85 bài học lập trình C  hay nhất PDF

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Interface C# Một Interface định nghĩa giao ước có tính chất cú pháp (syntactical contract) mà tất lớp kế thừa Interface nên theo Interface định nghĩa phần “Là gì” giao ước lớp kế thừa định nghĩa phần “Cách nào” giao ước Interface định nghĩa thuộc tính, phương thức kiện, mà thành viên Interface Các Interface chứa khai báo thành viên Việc định nghĩa thành viên trách nhiệm lớp kế thừa Nó thường giúp ích việc cung cấp Cấu trúc chuẩn mà lớp kế thừa nên theo Khai báo Interface C# Các Interface khai báo sử dụng từ khóa interface C# Nó tương tự khai báo lớp Theo mặc định, lệnh Interface puclic Ví dụ sau minh họa khai báo Interface C#: public interface ITransactions { // interface members void showTransaction(); double getAmount(); } Ví dụ Sau ví dụ minh họa trình triển khai Interface trên: using System.Collections.Generic; using System.Linq; using System.Text; using System; namespace InterfaceApplication { public interface ITransactions { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com // interface members void showTransaction(); double getAmount(); } public class Transaction : ITransactions { private string tCode; private string date; private double amount; public Transaction() { tCode = " "; date = " "; amount = 0.0; } public Transaction(string c, string d, double a) { tCode = c; date = d; amount = a; } public double getAmount() { return amount; } public void showTransaction() { Console.WriteLine("Transaction: {0}", tCode); Console.WriteLine("Date: {0}", date); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Console.WriteLine("Amount: {0}", getAmount()); } } class Tester { static void Main(string[] args) { Transaction t1 = new Transaction("001", "8/10/2012", 78900.00); Transaction t2 = new Transaction("002", "9/10/2012", 451900.00); t1.showTransaction(); t2.showTransaction(); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Transaction: 001 Date: 8/10/2012 Amount: 78900 Transaction: 002 Date: 9/10/2012 Amount: 451900 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ...http://vietjack.com/csharp/index.jsp Copyright © vietjack.com // interface members void showTransaction(); double getAmount(); } public class Transaction : ITransactions { private string tCode; private... public void showTransaction() { Console.WriteLine("Transaction: {0}", tCode); Console.WriteLine("Date: {0}", date); http://vietjack.com/ Trang chia sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp... 78900.00); Transaction t2 = new Transaction("002", "9/10/2012", 451900.00); t1.showTransaction(); t2.showTransaction(); Console.ReadKey(); } } } Khi code biên dịch th c thi, cho kết quả: Transaction: 001

Ngày đăng: 02/12/2017, 20:09

Mục lục

    Khai báo Interface trong C#

Tài liệu cùng người dùng

Tài liệu liên quan