官术网_书友最值得收藏!

Inheritance

The word inheritance means receiving or deriving something from something else. In real life, we might talk about a child inheriting a house from his or her parents. In that case, the child has the same power over the house that his parents had. This concept of inheritance is one of the pillars of OOP. In programming, when one class is derived from another class, this is called inheritance. This means that the derived class will have the same properties as the parent class. In programming terminology, the class from which another class is derived is called the parent class, while the classes that inherit from these are called child classes.

Let's look at an example:

public class Fruit {
public string Name { get; set; }
public string Color { get; set; }
}

public class Apple : Fruit {
public int NumberOfSeeds { get; set; }
}

In the preceding example, we used inheritance. We have a parent class, called Fruit. This class holds the common properties that every fruit has: a Name and a Color. We can use this Fruit class for all fruits.

If we create a new class, called Apple, this class can inherit the Fruit class because we know that an apple is a fruit. The properties of the Fruit class are also properties of the Apple class. If the Apple inherits the Fruit class, we don't need to write the same properties for the Apple class because it inherits these from the Fruit class.

主站蜘蛛池模板: 宿松县| 尚志市| 邢台县| 昭苏县| 永和县| 儋州市| 綦江县| 西青区| 鱼台县| 马关县| 偃师市| 科技| 通江县| 阿图什市| 牙克石市| 临夏县| 石台县| 封丘县| 桓仁| 东方市| 陇西县| 治县。| 漾濞| 宜丰县| 淮北市| 台中市| 中宁县| 蒙自县| 贵德县| 沂水县| 汉川市| 永安市| 溧水县| 沙坪坝区| 合山市| 锦屏县| 惠东县| 平和县| 白河县| 汉川市| 枣阳市|