OOP: When is it an object?

前端 未结 20 1375
悲哀的现实
悲哀的现实 2021-01-30 23:01

I\'m trying to understand object orientation. I understand it a little bit of course, but sometimes I\'m not 100% clear. How do you decide what should be turned into an object (

20条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 23:30

    The door knob would be a separate object in most cases, that can be assigned to a door-object.

    An excessive use of objects would say:
    There's an object for the lock, there is a color object for each color ("brownish" for the door, "silver" for lock and knob), and material objects ("wood" for the door and "steel" for knob and lock)
    Here you can see the difference between a class and an object:

    A class is an abstract (not in the sense of the programming language) form of something. You can refer something as a knob and everone knows what it is. You know that a knob has a color and a material.

    If you buy a knob, you have a concrete knob-object in your hand, with a specific color and material. You can now change the color of you knob object, e.g. paint it black.

    But there's a big diffenrence in programming objects and real-life objects. These basic examples only help to understand basic principles of OOP. You should let loose of this very soon!

    (For those who are curious: Is a rectangle a square or a square a rectangle?)

提交回复
热议问题