Tips 整理中====================================================================================
定义一个类:
class People def think puts "I'm Thinking" end end
继承一个类:
创建一个类的实例以及调用方法:class Worker < People def work puts "I'm Working" end end
worker = Worker.new() worker.think() worker.work()
面向对象: 封装,继承,多态在说明面向对象之前,我们需要了解Ruby变量中的局部变量,实例变量,类变量,常量和全局变量
局部变量: 小写字母或下划线开头 var 局部变量的作用域起始于声明处,结束于该声明所在的块、方法定义、类/模块定义的结尾
实例变量: @开头 @var 可以在类或子类的方法中引用实例变量。若引用尚未被初始化的实例变量的话,其值为nil
类变量: @@开头 @@var 在类的特殊方法、实例方法等处对类变量进行赋值和引用。类变量被类,类的子类和他们的实例对象共享,类变量是私有的,在类外无法直接访问,你只能通过实例方法和类方法去访问它。可以把类变量看作一种被类、子类以及它们的实例所共享的全局变量。常量: 大写字母开头 Var 常数的定义和初始化由赋值过程完成 若对已定义的常数进行赋值的话,会出现警告信息。若引用未定义的常数会引发NameError异常, 常量可以定义在类和模块中,不能定义在方法中 若想在外部访问类或模块中的常数时,要使用“::”操作符
全局变量(慎用!!): $开头 $var 全局变量可以在程序的任何地方加以引用。全局变量无需变量声明。引用尚未初始化的全局变量时,其值为nil。
class Variables $global_var = 'Global Variable' ConstantVar = 'Constant Variable' @@calss_var = 'Class Variable' @instance_var = 'Instance Variable' local_var = 'Local Variable' end
封装: ruby封装是强制性的:class People @name @age def initialize(name, age) @name = name @age = age end end pe = People.new("Jeremy",24) puts pe.name # undefined method `name' puts pe.age # undefined method `age'
在没有定义实例变量的attr_accessor之前,一个实例的实例变量是不能被访问的,也说明Ruby中的封装是强制性的。
继承: 类似于Java中的extends关键字, Ruby中用“<”符号来实现继承,且继承为单继承
class People def think puts "I'm Thinking" end end class Student < People def study puts "I'm Studying" end end Student.new.think # I'm Thinking Student.new.study # I'm Studying
多态:有待仔细研究
在Ruby中的方法有类方法和实例方法之分:
类方法(类似于Java中的静态方法):定义一个类方法有4种方式
class Person def Person.class_method1 # 用当前类名.方法名定义 puts 'this is Class method1' end def self.class_method2 # 用self.方法名定义 puts 'this is Class method2' end class << Person #这里要注意的是Ruby中的class关键字其实是用来定义一个类和打开一个类或者实例的方法列表 def class_method3 puts 'this is Class method3' end end class << self # self 是当前类对象,原理同上 def class_method4 puts 'this is Class method4' end end end Person.class_method1 Person.class_method2 Person.class_method3 Person.class_method4
实例方法:class Person def talk puts 'I can talk' end def introduce(name) puts 'my name is #{name}' # Ruby中单引号和双引号的区别:单引号直接把里面的内容输出而不进行解析,双引号会解析字符串然后返回解析后的结果,单引号性能优于双引号 puts "my name is #{name}" end end person = Person.new puts '--------talk--------------' person.talk() # Ruby方法调用可以不适用(), person.talk puts '--------introduce--------------' person.introduce('Jeremy')
特殊方法(Singleton Method)(与Java中的单例有区别):特殊方法指的是某实例所特有的方法,在Java中如果需要定义特殊方法则需要定义一个新的类,在Ruby中我们可以定义只属于某个特定对象的方法,这在Ruby中称为特殊方法(Singleton Method):
如果我们需要给某个特定对象定义一组特殊方法,我们可以这样实现:class SingletonTest def info puts "This is This is SingletonTest method" end end obj1 = SingletonTest.new obj2 = SingletonTest.new def obj2.info #定义一个特殊方法的第一种方式 puts "This is obj2" end obj1.info #This is This is SingletonTest method obj2.info #This is obj2
class SingletonTest def meth1 puts "This is meth1" end def meth2 puts "This is meth2" end end obj1 = SingletonTest.new obj2 = SingletonTest.new class << obj2 # 定义一个特殊方法的第二种方式 def meth1 puts "This is obj2's meth1" end def meth2 puts "This is obj2's meth2" end end obj1.meth1 #This is meth1 obj1.meth2 #This is meth2 obj2.meth1 #This is obj2's meth1 obj2.meth2 #This is obj2's meth2
存取控制(access control)Ruby中也Java类似,有三种存取控制的方式:
公有方法(Public Method): 方法在任何地方都可以被调用,这是方法的默认存取控制。除了initialize和initialize_cpoy方法,他们永远是私有方法。
保护方法(Protected Method): 方法只能被定义这个方法的类自己的对象和这个类的子类的对象所访问。
私有方法(private Method): 方法只能被定义这个方法的类的对象自己访问,即使是这个类的其他对象也不能访问。
Ruby中的保护方法和私有方法与一般面向对象程序设计语言的概念有所区别,保护方法的意思是方法只能被定义这个方法的类自己的对象和子类的对象访问,私有方法只能被对象自己访问。
access control 定义的第二种方式:class AccessControl # public def public_method puts 'this is public method' end def public_test(obj) obj.public_method end def protected_test(obj) obj.protected_method end def private_test(obj) obj.private_method end def self_private_test() private_method() private_method1() end # protected def protected_method puts 'this is protected method' end # private def private_method puts 'this is private method' end def private_method1 puts 'this is private method1' end # access control定义的一种方式 public :public_method protected :protected_method private :private_method, :private_method1 end ac1 = AccessControl.new ac2 = AccessControl.new ac1.public_test(ac2) #this is public method ac1.protected_test(ac2) #this is protected method ac1.self_private_test() #this is private method/this is private method1 ac1.private_test(ac2) # 错误,private method `private_method' called for #<AccessControl:0x17446c8> (NoMethodError)
class AccessControl public #在public下面的方法为public方法 ,默认是public方法 def public_method puts 'this is public method' end def public_test(obj) obj.public_method end def protected_test(obj) obj.protected_method end def private_test(obj) obj.private_method end def self_private_test() private_method() private_method1() end protected #在protected下面的方法是protected方法 def protected_method puts 'this is protected method' end private #在private下面的方法是private方法 def private_method puts 'this is private method' end def private_method1 puts 'this is private method1' end # public :public_method # protected :protected_method # private :private_method, :private_method1 end
Ruby Tips
最新推荐文章于 2016-03-07 20:47:20 发布