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

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.

主站蜘蛛池模板: 苍溪县| 翼城县| 革吉县| 上饶市| 锦州市| 德格县| 常宁市| 九寨沟县| 清河县| 屏山县| 祥云县| 临江市| 库尔勒市| 吉隆县| 和静县| 拉孜县| 加查县| 涿鹿县| 深泽县| 凭祥市| 宁乡县| 封开县| 楚雄市| 乌恰县| 平乡县| 蒙山县| 临沭县| 论坛| 临夏县| 朔州市| 惠水县| 盈江县| 绥化市| 澜沧| 临汾市| 宁晋县| 太仓市| 平陆县| 寻乌县| 广汉市| 郎溪县|