How to explain an object?

后端 未结 27 1621
广开言路
广开言路 2020-12-23 23:16

It\'s been years since I thought of this, but I am training some real juniors soon and need to explain what an object is to someone who doesn\'t know what it is.

B

27条回答
  •  眼角桃花
    2020-12-23 23:35

    I prefer using real-life examples that you can actually interact with: a dish-washer, a car, ...

    These things also happen to have a very strict interface: a car (in Europe :)) has an Acceleration pedal, a Break and a Clutch. You can ask for it's current speed.

    Encapsulation: no need to know how the engine works if you know the interface. A good thing that you don't have to tinker the engine yourself, too.

    Polymorphism: you can drive a Lada and a Porche using the same interface. This means you are a polymorphous driver :).

    Note: it's taken me years to differentiate the concept of an interface from inheritance. I would say: leave inheritance out for a while: most of the time code reuse is achieved better by composition (though that's a whole other discussion).

提交回复
热议问题