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

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.

主站蜘蛛池模板: 肇庆市| 石楼县| 洛川县| 北辰区| 个旧市| 静安区| 扶沟县| 航空| 远安县| 拉萨市| 铁力市| 兴安县| 科技| 昭平县| 手机| 锡林浩特市| 西平县| 潮安县| 邛崃市| 邛崃市| 云安县| 尉氏县| 安溪县| 平泉县| 乌鲁木齐县| 五家渠市| 景泰县| 江北区| 平乐县| 梁山县| 厦门市| 三亚市| 文成县| 枣强县| 岳阳市| 柳林县| 商南县| 许昌县| 东城区| 丽江市| 临泉县|