zoukankan      html  css  js  c++  java
  • ruby基础(四)

    ruby基础知识

    模块

    模块是ruby的特色功能之一。如果说类是事物的实体以及行为,那么模块表现的
    就是事物的行为部分,模块和类有以下两点不同:

    1. 模块不能拥有实例
    2. 模块不能被继承

    模块的使用方法

    module Mymodule
        #共同的方法
    end
    
    class Myclass1
        include Mymodule
        #Myclass1独有的方法
    end
    
    class Myclass2
        include Mymodule
        #Myclass2独有的方法
    end
    

    使用模块可以灵活的解决下面的问题

    1. 虽然两个类拥有相同的功能,但是不希望他们作为相同的种类来考虑
    2. Ruby不支持父类多重继承。

    ruby中模块会提供独立的命名空间,一个模块的方法、变量和另一个模块的方法、
    变量是不同的。

    模块函数

    使用模块名.方法名的形式来调用模块中方法,这样的方法称为模块函数。
    
    #检查文件是否存在
    p FileTest.exist?("/user/bin/ruby")  #false
    #圆周率常量
    p Math::PI                           #3.141592653589793
    

    使用include方法把模块内的方法名、常量名合并到当前的命名空间中

    include Math
    p PI  #3.141592653589793
    p sqrt 2   #1.4142135623730951
    

    创建模块

    module 模块名
       模块定义
    end
    

    模块的名字必须大写。eg:

    module Helloworld
      Version="1.0"
      def hello(name)
        puts "hello,#{name}."
      end
      #指定模块函数
      module_function :hello
    end
    
    Helloworld.hello("ruby")  #hello,ruby.
    p Helloworld::Version  #"1.0"
    
    
    include Helloworld  #包含模块
    
    p Version  #"1.0"
    
    hello "ruby" #hello,ruby
    

    和类一样,在模块中定义的常量可以用模块名访问

    p Helloworld::Version  #"1.0"
    

    方法调用
    如果只定义了方法,可以在模块内调用和包含该模块的语句中使用,但是不能以
    “模块.方法名”调用,如果想要这样使用,可以这样在模块中使用 module_function
    方法。

     module_function :hello
    

    以“模块.方法名”调用,方法中的self指向模块对象,如果将模块包含今类中
    ,则该方法变成了类的实例方法,self指向类的实例。

    module Helloworld
      Version="1.0"
      def hello
        self
      end
      #指定模块函数
      module_function :hello
    end
    
    p Helloworld.hello  #Helloworld
    
    
    module Helloworld
      Version="1.0"
      def hello
        self
      end
    end
    
    class Foo
      include Helloworld
      def aaa
        puts "I am aaa"
      end
    end
    
    foo=Foo.new()
    p foo.hello           #  #<Foo:0x0055c23667a338>
    

    想要知道类是否包含某个模块,可以使用include?方法

    module Helloworld
      Version="1.0"
      def hello
        self
      end
    end
    
    class Foo
      include Helloworld
      def aaa
        puts "I am aaa"
      end
    end
    
    p Foo.include? Helloworld  #true
    

    类的实例在调用方法的时候,会按照类、包含的模块、父类的顺序查找方法。
    被包含的模块相当于类的虚拟父类。
    使用ancestors方法和superclass方法调查类的继承关系

    module Helloworld
      Version="1.0"
      def hello
        self
      end
    end
    
    class Foo
      include Helloworld
      def aaa
        puts "I am aaa"
      end
    end
    
    p Foo.superclass  #Object
    p Foo.ancestors   #[Foo, Helloworld, Object, Kernel, BasicObject]
    
    p Object.ancestors #[Object, Kernel, BasicObject]
    p Object.superclass #BasicObject
    
    p BasicObject.superclass #nil
    p BasicObject.ancestors  #[BasicObject]
    
    p Class.superclass  #Module
    p Class.ancestors   #[Class, Module, Object, Kernel, BasicObject]
    
    p Module.superclass #Object
    p Module.ancestors #[Module, Object, Kernel, BasicObject]
    

    我们看出Object的父类是BasicObject.
    Class的父类是Module,Module的父类是Object.

    包含模块时查找方法的顺序

    1. 原类中方法优先级高于包含模块的
    2. 类中包含多个模块,优先使用最后包含的模块
    3. 多个模块,模块中包含模块,优先使用最后一个模块和模块中包含的模块。
    4. 相同模块第二次包含会被省略。

    可以使用ancestors查看方法的顺序。
    extend方法

    Obejct#extend的方法用来给对象扩展功能

    module Helloworld
      Version="1.0"
      def hello
        self
      end
    end
    
    str="ruby"
    str.extend Helloworld
    p str.hello  #"ruby"
    

    ruby中的类方法:Class类的实例方法,类对象的单例方法。使用extend
    方法给类添加类方法。
    eg:include给对象添加实例方法,extend给类添加类方法。

    module Instance_Method
      Version="1.0"
      def hello
        puts "hello"
      end
    end
    module Class_Method
      def say
        "Ruby"
      end
    end
    
    class Hello
      include Instance_Method
      extend  Class_Method
    end
    
    p Hello.say  #"Ruby"
    Hello.new.hello  #hello
    
  • 相关阅读:
    Redis设置生存时间或过期时间的相关命令
    .Net Core使用Redis
    Redis的数据类型
    Java集合 使用Collections
    Java集合 使用Iterator
    Java集合 使用Stack
    响应式常用样式汇总
    图片 文字 背景等响应式设置总结
    微信动态聊天工具
    第2章 栅格系统
  • 原文地址:https://www.cnblogs.com/yiluhuakai/p/9494109.html
Copyright © 2011-2022 走看看