简体   繁体   中英

Ruby - Relationship of Class/Objects/Methods

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 OOP 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 (eg a building) ... methods are you verbs (eg is built)!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM