How to explain an object?

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

    Sometimes it is hard for beginners to understand how objects relate to program execution. ("Ok, I have a Person object I can instantiate as 'Jerry', but where do I perform the instantiation? What creates Jerry? Ok, then what creates that? Where does it all start?")

    Try a universe analogy. The types in your program represent, at various levels of abstraction, all the possible objects that can exist in the universe. Make sure to show them the big bang (program entry point)! This will also show them why global variables in OOP don't make sense. Then you can dive right into OOP principles.

    0 讨论(0)
  • 2020-12-23 23:48

    Use real life examples.

    • Animal (interface)
    • Mammal (abstract class)
    • Dog (class)
    • Your dog (object instance)

    access levels: And your mom can't touch your privates, but your friends can.

    0 讨论(0)
  • 2020-12-23 23:48

    From my own experience, I think the idea of an object or class merely being an instance of a thing or a description of a thing can be quite adequate for an initial abstraction. What the thing has, i.e. members and/or properties, and can do, i.e. methods, are the next level of things to add. Then comes the inheritance, abstraction, encapsulation and polymorphism ideas as ways to round out the initial take of modelling that OOP tries to do. I came to OOP from a procedural coding background so the idea of customizable variable types does create a lot of different ideas that can be explored.

    0 讨论(0)
提交回复
热议问题