What Is Inheritance?

Chia sẻ: Nghia Tuan | Ngày: | Loại File: PDF | Số trang:1

lượt xem

What Is Inheritance?

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Thừa kế là gì? Có nói chung là rất nhiều sự nhầm lẫn giữa các lập trình viên như những gì được thừa kế. Một phần của sự cố này bắt nguồn từ thực tế là từ "thừa kế" tự nó có một số ý nghĩa tinh tế khác nhau.

Chủ đề:

Nội dung Text: What Is Inheritance?

  1. What Is Inheritance? There is generally a lot of confusion among programmers as to what inheritance is. Part of this trouble stems from the fact that the word “inheritance” itself has several subtly different meanings. If someone bequeaths something to you in a will, you are said to inherit it. Similarly, we say that you inherit half of your genes from your mother and half of your genes from your father. Both of these uses of the word inheritance have very little to do with inheritance in programming. Inheritance in programming is all about classification—it's a relationship between classes. For example, when you were at school, you probably learned about mammals, and learned that horses and whales are examples of mammals. Each does everything that a mammal does (it breathes air, gives birth to live young, and so on), but also has its own special features (a horse has hooves, unlike a whale). In Microsoft Visual C#, you could model this by creating two classes, one called Mammal and one called Horse, and declare that Horse inherits from Mammal. The inheritance would model that there is a relationship and would capture the fact that all horses are mammals. Similarly, you could also declare a class called Whale that also inherits from Mammal. Common functionality (such as breathing and giving birth) could be placed in the Mammal class; properties such as hooves or fins should be placed in the Horse or Whale class as appropriate.  
Đồng bộ tài khoản