What is the difference between class and instance methods?

后端 未结 18 2163
说谎
说谎 2020-11-21 11:55

What\'s the difference between a class method and an instance method?

Are instance methods the accessors (getters and setters) while class methods are pretty much ev

18条回答
  •  一向
    一向 (楼主)
    2020-11-21 12:32

    All the technical details have been nicely covered in the other answers. I just want to share a simple analogy that I think nicely illustrates the difference between a class and an instance:

    enter image description here

    A class is like the blueprint of a house: You only have one blueprint and (usually) you can't do that much with the blueprint alone.

    enter image description here

    An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.

提交回复
热议问题