Pertaining to Ruby, can someone state the relationship between Class, Objects, and Methods in layman's terms? And maybe even give an analogy?
New to O开发者_C百科OP and trying to grasp the concept.
note: OOP is independent of Ruby.
Classes are like recipes.
Objects are the actual food you prepare from a recipe.
Methods are ... don't really fit in this metaphor. But they are the behaviors an object can perform. Some might say methods are the messages an object will respond to.
A friend gave me this analogy:
"It's like a watch. The Class is the watch casing, it encompasses the inner workings of the watch (Objects and Methods). The hands are the Objects, they perform the function of the watch based on the movement of the gears (Methods). The gears and springs underneath the face are the Methods; they are the heart of the watch that make up the function of the hands (Objects). Without the gears (Methods) the hands (Objects) have no function. Without either of those, there's no need for the watch casing (Class) to exist."
Classes are your blueprints Methods are your tools (to build, to modify, to adjust) Objects are your buildings
Remember that your objects are your nouns (e.g. a building) ... methods are you verbs (e.g. is built)!!
精彩评论