Ruby: Object-Oriented-Programming 面向对象编程

OOP 面向对象编程

面向对象编程

Table of contents

  1. Class declaration
  2. Instance variables and methods
  3. Constructor method initialize
  4. Instance variables and Instance Methods
    1. Reader/Writer methods
    2. Other instance mtehods
  5. Class variables and Class methods
  6. Private methods
  7. Describing methods
  8. Inheritance
  9. Class instance Variables
  10. Dry principle
    1. Autogenerated Read/Writer Methods
  11. Modules
  12. Nested modules
  13. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值