Xem mẫu

  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.  
nguon tai.lieu . vn