Bài giảng Kỹ thuật lập trình: Chương 4 - ThS. Phạm Thanh An

74 458 1
Bài giảng Kỹ thuật lập trình: Chương 4 - ThS. Phạm Thanh An

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Kỹ thuật lập trình: Chương 4 - ThS. Phạm Thanh An

Chương 4: Kế thừa Đa hình (Inheritance and Polymorphism ) ThS Phạm Thanh An Khoa công nghệ thông tin Đại học Ngân hàng Tp Hồ Chí Minh Nội dung trình bày      Đặc biệt hóa tổng qt hóa Quan hệ kế thừa Đa hình Lớp trừu tượng Các lớp lồng Mục tiêu  Tìm hiểu mối quan hệ đối tượng giới thực  Cách thức mơ hình hóa mối quan hệ chương trình Đặc biệt hóa tổng quát hóa  Quan hệ (is a) đặc biệt hóa Khái quát hóa (Generalization) Đặc biệt hóa (Specialization) Động vật có vú Chó Lớp sở (Base class) Lớp dẫn xuất (Derived class) Kế thừa  Trong C#, quan hệ đặc biệt hóa, cài đặt thơng qua kế thừa  Khái niệm kế thừa sử dụng để khai báo lớp dựa lớp tồn Người Công nhân Khách hàng Sinh viên Kế thừa (tt)  Khai bao lớp kế thừa từ lớp khác ta sử dụng dấu “:” theo sau tên lớp kế thừa  Ví dụ: Chúng ta khai báo lớp người  Khai báo lớp sinh viên kế thừa lớp người  public class Sinhvien : Nguoi Kế thừa (tt) Lớp gọi lớp dẫn xuất (DerivedClass)  Lớp kế thừa gọi lớp sở (baseclass)  Một đối tượng lớp dẫn xuất xem đối tượng lớp sở  Một đối tượng lớp sở không xem đối tượng lớp dẫn xuất  Mọi lớp Net kế thừa từ lớp object (kế thừa không tường minh)  Kế thừa (tt)  Lưu ý: Lớp dẫn xuất kế thừa tất thành phần không private (non-private) lớp sở, bao gồm tất phương thức biến thành viên  Lớp dẫn xuất không kế thừa phương thức thiết lập lớp sở  Ví dụ : kế thừa Animal Cat Ví dụ 2: kế thừa public class Nguoi { private string ten; private string quequan; // etc public string Ten { get { return ten;} set { ten = value;} } public string quequan { get { return quequan; } set { quequan = value; } } public void Chaohoi(){ MessageBox.show (“Xin chao từ lớp Nguoi”); } } // kết thúc định nghĩa lớp Ví dụ: Ví dụ: Style Polygon Point Ví dụ : Car Ví dụ (tt) public class Car { // Aggregation uses instance of class outside of this class protected Door FrontRight; protected Door FrontLeft; protected Door RearRight; protected Door RearLeft; // inner class used to create objects // that are intrinsically linked to the class car protected class Engine { public int horsePower; } protected class Battery { public int voltage; } Ví dụ (tt) //Composition uses instances of objects of inner classes protected Engine TheEngine; protected Engine The Battery; public Car () { TheEngine = new Engine (); TheBattery = new Battery (); } } // kết thúc lớp Car public class Door { public int position; } Interface Trong chương trình lớn, việc thiết kế đặt lên hàng đầu  Việc thiết kế liên quan đến việc thiết kế lớp, thuộc tính, phương thức lớp  Rất hữu ích phân tách giao diện lớp cài đặt lớp  Giao diện lớp thành phần truy cập người sử dụng lớp, có nghĩa thành phần public  Các thành phần public cách để đoạn code bên lớp tương tác với đối tượng lớp  Interface (tt) public interface IFile { int delFile(); void disFile(); } public class MyFile : IFile { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } } Interface (tt) Interface thường xem lớp trừu tượng tinh khiết  Một định nghĩa interface, khai báo phương thức, thuộc tính, events cài đặt số lớp  Trong khai báo interface có tiêu đề, khơng có cài đặt  Các lớp cài đặt Interface phải cài đặt thành phần khai báo Interface  Ví dụ public interface IFile { int delFile(); void disFile(); } public class BaseforInterface { public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); } } Ví dụ (tt) public class MyFile : BaseforInterface, IFile { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } } Interface (tt)  Một lớp kế thừa lớp, cài đặt nhiều interface public interface IFileTwo { void applySecondInterface(); } Interface (tt) public class MyFile : BaseforInterface, IFile, IFileTwo { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } public void applySecondInterface() { System.Console.WriteLine ("ApplySecondInterface Implementation!"); } } Cài đặt Interface tường minh  Sử dụng hai phương thức chung tên interfaces public interface IFile { int delFile(); void disFile(); } public interface IFileTwo { void applySecondInterface(); void disFile(); } Cài đặt Interface tường minh (tt) public class MyFile : BaseforInterface, IFile, IFileTwo { void IFile.disFile() { System.Console.WriteLine ("IFile Implementation of DisFile"); } void IFileTwo.disFile() { System.Console.WriteLine ("IFileTwo Implementation of DisFile"); } } Q&A ... hóa Quan hệ kế thừa Đa hình Lớp trừu tượng Các lớp lồng Mục tiêu  Tìm hiểu mối quan hệ đối tượng giới thực  Cách thức mơ hình hóa mối quan hệ chương trình Đặc biệt hóa tổng quát hóa  Quan hệ... string ten; private string quequan; // etc public string Ten { get { return ten;} set { ten = value;} } public string quequan { get { return quequan; } set { quequan = value; } } public void Chaohoi(){... lập lớp dẫn xuất  Mặc định, lớp dẫn xuất gọi phương thức thiết lập mặc định lớp sở (phương thức thiết lập tham số)  Gọi tường minh phương thức thiết sở với từ khóa base Phương thức thiết lập

Ngày đăng: 27/05/2014, 14:58

Từ khóa liên quan

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

Tài liệu liên quan