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

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.

主站蜘蛛池模板: 沛县| 绥棱县| 福泉市| 柯坪县| 大兴区| 丽江市| 临湘市| 泊头市| 沁阳市| 梅河口市| 大城县| 澄城县| 蓝山县| 亳州市| 乳山市| 兰坪| 阳西县| 闸北区| 河北区| 林口县| 鲁甸县| 罗甸县| 元朗区| 安平县| 漳平市| 来宾市| 仁怀市| 汉阴县| 内丘县| 阿坝县| 兴仁县| 神农架林区| 新源县| 北票市| 山阴县| 阿克苏市| 藁城市| 大荔县| 华容县| 龙泉市| 保定市|