Class
-
.new
:实例化的关键字
-
initialize
:当前类实例化的时候会首先被调用,可以不定义
- class和module可以重新打开
- 方法可以重定义,前一个会被覆盖
- 举例:
- 手动设置读写:
class USer
def initialize name,age
@name = name
@age = age
end
// getter(实例变量不能直接访问,因此需要getter)
def name
@name
end
def age
@age
end
// setter
def name= name
@name = name
end
def @age= age
@age = age
end
end
- ruby内部自带的读写方法:
attr_accessor
最强大,一行就可以实现读写操作
class User
attr_accessor :name
attr_accessor :age
// attr_reader :name, :age
// attr_reader :age
// attr_writer :name, :age
// attr_writer :age
def initialize name, age
@name = name
@age = age
end
def say_hi
// @name 是实例变量,age 相当于返回实例变量@age的方法
puts "hello,I am #{@name},age is #{age}"
end
end
class User
attr_accessor :name
attr_accessor :age
@@counter = 0
def initialize name,age
@name = name
@age = age
@@counter += 1
end
// def User.get_counter
def self.get_counter
@@counter
end
end
-
public
:实例可以访问
-
protected
:实例不可以访问,继承的子类可以访问
-
private
:限制继承的子类也不可访问
class User
...
end
User.class => Class
define_method :hello do
p "hello"
end
class User
def self.setup_accessor var
define_method var do
instance_variable_get "@#{var}"
end
define_method "#{var}=" do |value|
instance_variable_set "@#{var}", value
end
end
setup_accessor :name
setup_accessor :age
def initialize name,age
@name = name
@age = age
end
end
- 继承:
<
;查看继承链:Array.ancestors
class User
def initialize name,age
@name = name
@age = age
end
def panels
@panels ||= ['Profile','Product']
end
end
class Admin < User
def panels
@panels ||= ['Profile','Product','Manage Users','System Setup']
end
end
- 查看一个类所属的父类:
Admin.superclass // User
,ruby里一个类只能继承一个类,不能继承多个类
-
super
会调用父类的同名方法
class User
def initialize name,age
@name = name
@age = age
end
def panels
@panels ||= ['Profile','Product']
end
end
class Admin < User
def panels
super
@panels.concat ['x','y','z']
end
end
-
self
要看作用域
,在实例方法
里代指的是当前实例
,在类本身里
就代指当前类这个Class实例
- 定义多个类方法:
class User
def initialize name,age
@name = name
@age = age
end
// fn1,fn2 都是类方法
class << self
def fn1
end
def fn2
end
end
end
Modules
- namespace:
::
来访问,例如include A::b
- mixin:实现类似于多类继承的功能
- storage
-
module
:声明一个module
-
include
:把一个module的方法注入当前类的继承链后面,成为实例方法
-
prepend
:把一个module方法注入当前类的继承链前面,成为实例方法
-
extend
:把一个module的方法注入为类方法
-
included
:当模块被include时会被执行,同时会传递当前作用域的self对象
module Management
def company_notify
'company_notify'
end
end
class User
def initialize name,age
@name = name
@age = age
end
def panels
@panels ||= ['Profile','Product']
end
end
class Admin < User
include Management
def panels
super
@panels.concat ['x','y','z']
end
end
class Staff < User
include Management
def panels
super
@panels.concat ['a','b','c']
end
end
- Module本质上也是一个class,可以看作去除实例化功能(new)的class
- module会在引用这个module的类的
ancestors
查找链方法中查找出
- demo
module M1
def m
"M1"
end
end
module M2
def m
"M2"
end
end
class User
include M1
include M2
def m
p super
"User"
end
end
p User.ancestors // [User,M2,M1,Object,Kernel,BasicObject]
user = User.new
user.m // "M2" "User"
- 模块之间也可以
include
和super
- 模块也支持类似类方法的定义
module Management
def self.progress
'progress'
end
def company_notify
'company_notify'
end
end
p Management.progress // 'progress'
-
class_eval
:只有类才能调用,打开一个类的作用域,进行重定义
class User
end
User.class_eval do
attr_accesstor :name
def hello
'hello'
end
end
User.new.hello
module Management
// self 绑定成了当前模块了
def self.hi
"hi"
end
end
class User
include Management
end
User.hi => error
Management.hi => "hi"
module Management
def self.included base
base.extend ClassMethods
base.class_eval do
setup_attribute
end
end
module ClassMethods
def setup_attribute
p 'setup_attribute'
end
end
end
class User
include Management
end
// 'setup_attribute'
-
instance_eval
:是所有实例的方法,打开类的实例作用域
class User
end
User.class_eval do
def hello
'hello'
end
end
User.instance_eval do
def hi
'hi'
end
end
p User.hi
p User.new.hello