标签:
By encapsulating all the logic for an object, whether it’s a Dog
or a User
or an IceCreamShop
, you are able to keep all of the logic and responsibilities of your object within its own scope. This lets you avoid having to worry about other objects using methods that they shouldn’t have access to.
Let’s take one last look at how your Dog
methods took advantage of encapsulation. Let’s say you also had some methods for your pet parrot in there as well. Here’s what your code might have looked like:
def drink_water_from_bowl(dog) # code for method end def wants_to_play?(dog) # code for method end def fly_away(parrot) # code for method end def wag_tail(dog) # code for method end
And here’s what it would look like using OOP:
class Dog def drink_water_from_bowl # code for method end def wants_to_play? # code for method end def wag_tail # code for method end end class Parrot def fly_away # code for method end end
Not only is it much more apparent which actions are supposed to be performed by which objects, but now you won’t have to worry about accidentally calling fly_away
on your dog. Who knows what could have happened there!
To build off the benefits of encapsulation, OOP helps keep your code readable. By keeping all of its instance methods tabbed in one level deep, it becomes much simpler to skim through your files.
To take this organization to the next level, let’s address one last convention when it comes to classes. As a rule of thumb, each class should exist in its own file. This way, when you’re working on an application that has multiple classes, like ClothingStore
and Customer
, you know exactly where to look to make changes to your code.
These files should have the same name as the class they contain, with a few slight changes. Instead of naming your files in upper camel case (ClothingStore.rb
), you should name them using snake case, like you would name a variable (clothing_store.rb
).
But how do you get those files to interact with each other? Files don’t just magically know that there are other files that need to be included in your project. You’ll need to explicitly require those files.
Let’s say you’ve got three files: clothing_store.rb
, customer.rb
, and app.rb
. To include the contents of the first two files in the last file, you can use require_relative
. Let’s see that in action.
# app.rb require_relative "clothing_store.rb" require_relative "customer.rb" # The rest of your code...
This code will look for the files you specify relative to the current file. That means that ifcustomer.rb
was one directory level above the current directory, you’d writerequire_relative “../customer.rb"
.
Additionally, Ruby allows you to omit the .rb
file extension by default. So your app.rb
file can be written instead as:
# app.rb require_relative "clothing_store" require_relative "customer" # The rest of your code...
Integer
to create new types of objects.class
method.reverse
for Strings.methods
method.@
.attr_reader
, attr_writer
, and attr_accessor
are handy shortcuts for getter and setter methods.initialize
to take care of any initial set-up for your classes.require_relative
.
Encapsulation and Requiring Files
标签:
原文地址:http://www.cnblogs.com/iwangzheng/p/5450527.html