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

Abstraction

If something is abstract, it means that it doesn't have an instance in reality but does exist as an idea or concept. In programming, we use this technique to organize our thoughts. This is one of the pillars of OOP. In C#, we have abstract classes, which implement the concept of abstraction. Abstract classes are classes that don't have any instances, classes that implement the abstract class will implement the properties and methods of that abstract class. Let's look at an example of an abstract class, as shown in the following code:

public abstract class Vehicle {
public abstract int GetNumberOfTyres();
}

public class Bicycle : Vehicle {
public string Company { get; set; }
public string Model { get; set; }
public int NumberOfTyres { get; set; }

public override int GetNumberOfTyres() {
return NumberOfTyres;
}
}

public class Car : Vehicle {
public string Company { get; set; }
public string Model { get; set; }
public int FrontTyres { get; set; }
public int BackTyres { get; set; }

public override int GetNumberOfTyres() {
return FrontTyres + BackTyres;
}
}

In the preceding example, we have an abstract class called Vehicle. It has one abstract method, called GetNumberOfTyres(). As it is an abstract method, this has to be overridden by the classes that implement the abstract class. Our Bicycle and Car classes implement the Vehicle abstract class, so they also override the abstract method GetNumberOfTyres(). If you take a look at the implementation of these methods in the two classes, you will see that the implementation is different, which is due to abstraction.

主站蜘蛛池模板: 道孚县| 宣恩县| 疏附县| 田东县| 宕昌县| 天津市| 吉林省| 定南县| 峡江县| 博罗县| 许昌市| 崇阳县| 霸州市| 汽车| 合阳县| 桑植县| 石门县| 五河县| 乌兰浩特市| 赤壁市| 井冈山市| 呼和浩特市| 宜川县| 江山市| 喀喇沁旗| 子洲县| 泗水县| 四会市| 凉山| 汝州市| 融水| 兴宁市| 星座| 运城市| 韩城市| 宁城县| 泗洪县| 道孚县| 玉田县| 即墨市| 富源县|