How to explain an object?

后端 未结 27 1614
广开言路
广开言路 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:34

    I might start with anonymous types or something like JSON to demonstrate that objects are just collections of properties. Talk about cohesion.

    What if we want a bunch of these objects? Classes/constructors/instances.
    What about functionality? Methods.

    Then demonstrate the problems that occur from a lack of encapsulation. Show how getters/setters/private fields alleviate the problem (maybe move to a typed language if you've been using JSON)...

    Then show how inheritance allows code reuse--maybe show a sample of inheritance by actually re-typing (with the keyboard) the functionality to demonstrate what a pain it is. Show that you can change something in one authoritative place when things are well-designed.

    Then introduce polymorphism--because inheritance is sometimes insufficient. Include interfaces here.

    Try to use real-world examples--either from the code-base they'll be working on or from your own portfolio. As you go, demonstrate refactoring and other good practices. "Hey, I keep repeating this pattern--I should consolidate it somehow".

提交回复
热议问题