What does abstraction mean in programming?

前端 未结 7 661
时光说笑
时光说笑 2020-12-07 08:40

I\'m learning python and I\'m not sure of understanding the following statement : \"The function (including its name) can capture our mental chunking, or abstraction

7条回答
  •  遥遥无期
    2020-12-07 09:14

    A great way to understand abstraction is through abstract classes.

    Say we are writing a program which models a house. The house is going to have several different rooms, which we will represent as objects. We define a class for a Bathroom, Kitchen, Living Room, Dining Room, etc.

    However, all of these are Rooms, and thus share several properties (# of doors/windows, square feet, etc.) BUT, a Room can never exist on it's own...it's always going to be some type of room.

    It then makes sense to create an abstract class called Room, which will contain the properties all rooms share, and then have the classes of Kitchen, Living Room, etc, inherit the abstract class Room.

    The concept of a room is abstract and only exists in our head, because any room that actually exists isn't just a room; it's a bedroom or a living room or a classroom.

    We want our code to thus represent our "mental chunking". It makes everything a lot neater and easier to deal with.

提交回复
热议问题