Tính kế thừa trong C | 85 bài học lập trình C hay nhất PDF

7 189 0
Tính kế thừa 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 Tính kế thừa C# Một khái niệm quan trọng lập trình hướng đối tượng Tính kế thừa (Inheritance) Tính kế thừa cho phép định nghĩa lớp điều kiện lớp khác, mà làm cho dễ dàng để tạo trì ứng dụng Điều cung cấp hội để tái sử dụng tính code thời gian thực thi nhanh Khi tạo lớp, thay viết tồn thành viên liệu hàm thành viên mới, lập trình viên nên kế thừa thành viên lớp tồn Lớp tồn gọi Base Class - lớp sở, lớp xem Derived Class – lớp thừa kế Ý tưởng tính kế thừa triển khai mối quan hệ IS-A (Là Một) Ví dụ, mammal IS A animal, dog ISA mammal, dog IS-A animal, Lớp sở (Base Class) Lớp thừa kế (Derived Class) C# Một lớp kế thừa từ lớp khác, nghĩa là, kế thừa liệu hàm từ nhiều Lớp Interface sở Cú pháp để tạo lớp kế thừa C# là: class { } class : { } Xét lớp sở Shape lớp kế thừa Rectangle sau: using System; namespace InheritanceApplication { class Shape http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class class Rectangle: Shape { public int getArea() { return (width * height); } } class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object Console.WriteLine("Total area: {0}", Rect.getArea()); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Total area: 35 Khởi tạo Lớp sở (Base Class) C# Lớp kế thừa (Derived Class) C# kế thừa biến thành viên phương thức thành viên từ lớp sở Vì thế, đối tượng lớp cha nên tạo trước lớp phụ tạo Bạn cung cấp thị để khởi tạo lớp phụ danh sách khởi tạo thành viên Chương trình ví dụ sau minh họa cách khởi tạo Lớp sở (Base Class) C#: using System; namespace RectangleApplication { class Rectangle { //member variables protected double length; protected double width; public Rectangle(double l, double w) { length = l; width = w; } public double GetArea() { return length * width; } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }//end class Rectangle class Tabletop : Rectangle { private double cost; public Tabletop(double l, double w) : base(l, w) { } public double GetCost() { double cost; cost = GetArea() * 70; return cost; } public void Display() { base.Display(); Console.WriteLine("Cost: {0}", GetCost()); } } class ExecuteRectangle { static void Main(string[] args) { Tabletop t = new Tabletop(4.5, 7.5); t.Display(); Console.ReadLine(); } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com } } Khi code biên dịch thực thi, cho kết quả: Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5 Đa kế thừa C# C# không hỗ trợ đa kế thừa Tuy nhiên, bạn sử dụng Interface để triển khai đa kế thừa Ví dụ sau minh họa cách sử dụng Interface để triển khai đa kế thừa C#: using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Base class PaintCost public interface PaintCost { int getCost(int area); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com } // Derived class class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 70; } } class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object Console.WriteLine("Total area: {0}", Rect.getArea()); Console.WriteLine("Total paint cost: 0" , Rect.getCost(area)); Console.ReadKey(); } } } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Khi code biên dịch thực thi, cho kết quả: Total area: 35 Total paint cost: $2450 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Console.ReadKey(); } } } Khi code biên dịch th c thi, cho kết quả: Total area: 35 Khởi tạo Lớp sở (Base Class) C# Lớp kế thừa (Derived Class) C# kế thừa biến... public interface PaintCost { int getCost(int area); http://vietjack.com/ Trang chia sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com } // Derived class class... sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com } } Khi code biên dịch th c thi, cho kết quả: Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5 Đa kế thừa

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

Từ khóa liên quan

Mục lục

  • Tính kế thừa trong C#

    • Lớp cơ sở (Base Class) và Lớp thừa kế (Derived Class) trong C#

    • Khởi tạo Lớp cơ sở (Base Class) trong C#

    • Đa kế thừa trong C#

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

Tài liệu liên quan