OOP 面向对象编程
Table of contents
- Class declaration
- Instance variables and methods
- Constructor method initialize
- Instance variables and Instance Methods
- Class variables and Class methods
- Private methods
- Describing methods
- Inheritance
- Class instance Variables
- Dry principle
- Modules
- Nested modules
- Requiring files
Class declaration
class Post
# ...
end
post = Post.new()
post.class.superclass #=> Object
Instance variables and methods
class Post
def initialize(title, content)
@title = title
@content = content
end
end
post = Post.new(title, content)
Constructor method initialize
def new(*args, &block)
obj = allocate
obj.initialize(*args, &block)
obj
end
# all arguments passed to new gets passed to initialze
# initialize is the only part of new that need to overwrite
Instance variables and Instance Methods
Instance variables are always private.
Instance methods are public by default.
Reader/Writer methods
class Post
def initialize(title, content)
@title = title
@content = content
end
def title
@title
end
def title=(title) # "title=(title)" is the name of the function
return (p 'Too long') if title.length > 2
@title=title
end
end
Other instance mtehods
def talk(message, prefix="echo-") # name(arguments, arguments with default values)
puts "#{prefix}#{message}"
end
Class variables and Class methods
Similar to static methods and variables in Java
class Person #method called on the class itself
@@average_life = '80 years' ## double @ at beginning for class variables
def self.compare(p1,p2) # starts with self
#...do something
return #something
end
def self.average_life
@@average_life
end
def self.average_life=(amount)
@@average_life = amount
end
def instance_methods; end
end
p Person.compare # cannot call any instance methods
Private methods
Private keyword
class Person
def age_increase
grow
end
private
# all methods below are private
def grow
@age += 1
end
end
Describing methods
# instance methods
ClassName#instance_method
# i.e. String#capitalize
# class methods
ClassName::class_method
# i.e. Math::tan
Inheritance
Done with < operator.
Both public and private methods are inherited
If one descendent overwrites a class variable, its value in the parent class is also changed.
class Bird; end
class Penguine < Bird; end
# inheriatnce bottom logic =>
Penguine = Class.new(super_class = Bird) do
...
end
Each class is an object; an instance of the class Class
Object has methods (class methods)
Object has attribuets (instance variables)
Class instance Variables
Defined outside methods.
Class instance variables are not inherited.
class Bird
@place = "everywhere"
def self.place
@place
end
end
class Penguine < Bird
@place = Antarcitica
end
p Bird.place #=> "everywhere"
p Penguine.place #=> "Antarcitica"
Dry principle
Don’t Repeat Yourself!!!
Auto-generated reader/write methods
class Person
# attr_reader :name =>
# attr_writer :name => both are replaced by
attr_accessor :name, :age, :language
def initialize(name, age, language)
@name = name
@age = age
@language = language
end
end
person = Person.new ('Sanj', 21, 'Ruby')
p person.name #=> "Sanj"
p person.age #=> 21
p person.language #=> "Ruby"
Modules
Dry across classes.
Modules are methods only bundles.
Cannot be instantiated
Can only be included or extended in other class
- Included: methods are used as instance methods
- extended: methods are used as class methods
- Seperate instance methods module and class methods module
module InstanceModule
def instance_module_method
'Hello World, I am instance method'
end
end
module ClassModule
def class_module_method
'Hello World, I am class method'
end
end
class Bird
include InstanceModule
extend ClassModule
end
p Bird.class_module_method #=> 'Hello World, I am class method'
p Bird.new.instance_module_method #=> 'Hello World, I am instance method'
Nested modules
module MyModule
module InstanceMethods
def intance_method
p 'Instance Method'
end
end
module ClassMethods
def class_method
p 'Class Method'
end
end
end
# using :: operatire to access a module in another module
class MyClass
include MyModule::InstanceMethods
extend MyModule::ClassMethods
end
Requiring files
require './foo'
# Ruby using require keyword to import other files, put it at top of the file to run first
# requiring a file without path name, it will try to import a gem of the name