`
世说新语
  • 浏览: 23378 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Ruby Programming Language学习笔记之对象、类、模块(初稿)

阅读更多

对象、类、模块

1.1、封装

1.1.1类层次结构

Object:顶级类
Kernel:mix-in到Object中的模块
Class:产生类对象的类

Module Module是Class的父类

Classes, modules, and objects are interrelated. In the diagram that follows, the vertical arrows represent inheritance, and the parentheses meta-classes. All metaclasses are instances of the class `Class ’.

                          +------------------+
                          |                  |
            Object---->(Object)              |
             ^  ^        ^  ^                |
             |  |        |  |                |
             |  |  +-----+  +---------+      |
             |  |  |                  |      |
             |  +-----------+         |      |
             |     |        |         |      |
      +------+     |     Module--->(Module)  |
      |            |        ^         ^      |
 OtherClass-->(OtherClass)  |         |      |
                            |         |      |
                          Class---->(Class)  |
                            ^                |
                            |                |
                            +----------------+



1.1.2总论

Ruby所有的东西都是对象,而每个对象都是某个类的实例。类本身也是对象,称为类对象。既然类是对象,那么产生类对象的类是什么呢,就是Class类。
类对象有自己的方法、状态和ID。它不与它的实例分享这些信息。这一点与Java不同,Java中的static变量/方法就是类变量/方法,它们是类和类的所有实例共享的。


模块与类的主要区别是:不能实例化、不能继承(可以互相mix-in)
类可以mix-in多个module,但只能有一个父类

对象/类/模块都可以被重新打开,添加内容。打开类/模块与定义它们是一样的,但打开对象就需要用<<
可以给对象添加方法,称为singleton method(不推荐如此)。而在类中编写、可被类的所有实例使用的方法称为实例方法。

实体或事物最好建模为类,而实体说事物的特性或行为最好建模为模块。因此倾向于用名称作为类名,而用形容词作为模块名。
module的思想类似AOP(面向切面编程)。

module和class可以互相多层嵌套,用::引用,此时module和class的作用相当于namespace。





1.1.3类

类方法(类实例方法)
定义类方法用<<符号,以下是重新打开一个类
class << Point
     def class_method1      # This is an instance method of the eigenclass.
     end                               # It is also a class method of Point.
     def class_method2
     end
end
如果是在类定义的时候则可以这样
class Point
     # instance methods go here
     class << self
         # class methods go here as instance methods of the eigenclass
     end

    def Point.class_method1
    end

    def this.class_method2
    end

end
下面2种方法是一样的,这说明<<用于向对象添加方法 ,这个对象包括类对象和普通对象。
obj = T1.new

def obj.test
....
end

class << obj
....
end

类实例变量 :在类定义中定义的@variable_name

class Point
     # Initialize our class instance variables in the class definition itself
     @n = 0 # How many points have been created
     @totalX = 0 # The sum of all X coordinates
     @totalY = 0 # The sum of all Y coordinates

     def initialize(x,y) # Initialize method
         @x,@y = x, y # Sets initial values for instance variables
     end

     def self.new(x,y) # Class method to create new Point objects
          # Use the class instance variables in this class method to collect data
          @n += 1 # Keep track of how many Points have been created
         @totalX += x # Add these coordinates to the totals
         @totalY += y
         super # Invoke the real definition of new to create a Point
         # More about super later in the chapter
     end

     # A class method to report the data we collected
     def self.report
          # Here we use the class instance variables in a class method
          puts "Number of points created: #@n"
          puts "Average X coordinate: #{@totalX.to_f/@n}"
          puts "Average Y coordinate: #{@totalY.to_f/@n}"
     end
end


类变量
:@@variable_name
类变量一般直接在类中定义,而不在任何方法中定义;对与类方法和实例方法都是可见的。
class Point
     # Initialize our class variables in the class definition itself
     @@n = 0 # How many points have been created
     @@totalX = 0 # The sum of all X coordinates
     @@totalY = 0 # The sum of all Y coordinates
   
     def initialize(x,y) # Initialize method
          @x,@y = x, y # Sets initial values for instance variables
          # Use the class variables in this instance method to collect data
          @@n += 1 # Keep track of how many Points have been created
          @@totalX += x # Add these coordinates to the totals
          @@totalY += y
     end

     # A class method to report the data we collected
     def self.report
         # Here we use the class variables in a class method
        puts "Number of points created: #@@n"
          puts "Average X coordinate: #{@@totalX.to_f/@@n}"
          puts "Average Y coordinate: #{@@totalY.to_f/@@n}"
     end
end

类(实例)方法是类对象独有的,类实例变量也是类对象独有的,只有类(实例)方法可以访问。
类变量是类及其所有实例共有的。

实例方法:

实例变量: 在实例方法中定义的@variable_name,一般在initialize方法中初始化。

单例方法: 对象特有的(而不是在类中定义的)方法

常量: 给常量名重新赋值和修改常量所引用的对象,这个差别非常重要。Ruby中存在2中改变:改变标识符到对象的映射,改变对象的内容或状态。对于常量来说,前者是可以的,但会引起警告,也是不推荐的。后者则是完全没有问题的。
常量用::引用


1.1.4模块

Module是模块方法、实例方法、常量(类和模块也是常量)和类变量(@@variable_name)的集合。
A module is a named group of methods, constants, and class variables.

当一个类include一个模块的时候,模块的实例方法可见,模块的模块方法不可见。相反的,调用模块方法的语法是Module::method,而要调用模块的实例方法则必须通过include该模块的类的具体实例来调用。
A
Module is a collection of methods and constants . The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included , module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not. (See Module#module_function )

模块方法

module Base64
     def self.encode
     end

     def self.decode
     end
end
或者
module Base64
     def Base64.encode
     end

     def Base64.decode
     end
end

实例方法
module Base64
     def encode
     end

     def decode
     end
end

1.1.5对象

生成一个对象
obj=Object.new

给对象定义行为
def obj.talk
end
或者
class << obj
    def talk
    end
end
这些方法叫做单例方法。

1.1.6方法可见性

public/protected/private
ruby中的可见性只针对method 。变量统一都是private的,常量统一都是public的,它们没有可见性这一说。

对实例变量来说,它并不是在类定义中创建的,它是在运行时首次被赋值的时候创建的。它超脱于类之外,只是附着在运行时对象上,因此不能用public/protected/private这些编译时的方法来限定它。同事实例变量也不在类的继承体系中。原因相同。

public/protected/private都是在Module类中的方法,它们不是关键字 。它们可以在Class和Module 中使用,但不能在Object中使用。也就是说我们通过:
class << obj
    def talk
    end
end
添加单例方法时不能使用public/protected/private

public
默认

private
对实例方法可见
对于private方法m,任何时候都只能用m调用,不能用o.m或self.m调用

protected
对于protected方法m,任何时候都可以用m或o.m或self.m调用,因此常用于同一个类的不同实例之间互相访问数据。原理如下
A protected method defined by a class C may be invoked on an object o by a method in an object p if and only if the classes of o and p are both subclasses of, or equal to, the class C.

private_class_method
public_class_method

用于处理类方法的可见性问题

1.2、继承

方法搜索的顺序:当前类-->当前类mix-in的module(与声明顺序相反)-->父类-->父类类mix-in的module(与声明顺序相反)
super:获取并执行在方法查找路径上下一个 匹配该方法的名的方法。对类方法和实例方法同样有效。
this

public/protected/private的方法都会被继承(与Java不同)

实例方法 :继承

实例变量: 不继承。实例变量超脱于类及其继承体系之外。
对实例变量来说,它并不是在类定义中创建的,每一个对象都有一个实例变量集,集合中的实例变量是在运行时首次被赋值的时候创建的。它超脱于类及其继承体系 之外,是完全的运行时概念,只是附着在运行时对象上。有时实例变量看起来是被继承了的,这只是表面现象,内在的原因是实例变量在某些方法中被首次赋值从而 被创建,而这些方法被继承了,所以看起来像是实例变量也被继承了。代码如下:
class Point3D < Point
     def initialize(x,y,z)
          super(x,y)
          @z = z;
     end

     def to_s
          "(#@x, #@y, #@z)" # Variables @x and @y inherited?
     end
end

Point3D.new(1,2,3).to_s # => "(1, 2, 3)"

类方法 :继承

类变量: 继承
在定义该类变量的类及其所有子类中共享同一个变量。

常量: 继承
如果子类常量与父类常量同名,则子类和父类都会单独拥有2个常量,如:
Point::ORIGIN and Point3D::ORIGIN


抽象类:父类中可引用尚未实现的子类方法。
# This class is abstract; it doesn't define greeting or who
# No special syntax is required: any class that invokes methods that are
# intended for a subclass to implement is abstract.
class AbstractGreeter
     def greet
          puts "#{greeting} #{who}"
     end
end

# A concrete subclass
class WorldGreeter < AbstractGreeter
     def greeting; "Hello"; end
     def who; "World"; end
end

WorldGreeter.new.greet # Displays "Hello World"

评论

相关推荐

    The Ruby Programming Language 介绍

    在《The Ruby Programming Language》这本书中,作者深入浅出地介绍了Ruby的各个方面,从基础语法到高级特性的应用,是学习和理解Ruby的宝贵资源。书中涵盖了类和对象、模块、方法、变量、控制结构、异常处理、正则...

    The Ruby Programming Language (part 1)

    2008新作,是英文版啊. Ruby的作者的作品, part.1 一共两个压缩包 Part 2在http://download.csdn.net/source/424089

    The Ruby Programming Language

    《The Ruby Programming Language》是一本详细介绍Ruby语言的权威书籍,由Ruby之父Yukihiro Matsumoto(松本行弘)与著名技术作家David Flanagan共同撰写。本书不仅适合初学者作为入门教材,同时也为经验丰富的...

    The Ruby Programming Language 2008 .pdf

    - **面向对象编程**:介绍了Ruby中的类、对象、继承等面向对象编程的核心概念。 - **高级特性**:涵盖了模块、元编程、异常处理等更高级的主题。 - **Web开发**:由于Ruby on Rails框架的流行,书中还包含了与Web...

    The Ruby Programming Language PDF

    1. Ruby语言介绍:文件标题《The Ruby Programming Language》表明了文档是关于Ruby编程语言的。Ruby是一种流行的开源、动态的面向对象脚本语言,由松本行弘(Yukihiro "Matz" Matsumoto)设计。它以简洁易读的语法...

    The Ruby Programming Language (part 2)

    第二个压缩包 The Ruby Programming Language 一共两个压缩包 Part 1在http://download.csdn.net/source/424086

    ruby笔记1ruby笔记1ruby笔记1

    综合以上分析,我们可以期待这份压缩包内的笔记涵盖了Ruby的基础知识,如变量、数据类型、控制结构,以及进阶主题,如类和模块、方法、异常处理等。同时,它还包含了作者在学习过程中的情感体验,这对于其他学习者来...

    Ruby Programming

    《Programming Ruby》被誉为是最好的Ruby编程书籍之一,它不仅详尽地介绍了Ruby语言的基础知识,还深入探讨了Ruby的核心概念和技术细节。这本书由David Thomas、Andy Hunt、Thomas A.EW Matthews和David Heinemeier ...

    《Ruby Programming—向Ruby之父学程序设计(第2版)》电子书

    书中会解释类、对象、继承、方法、模块(用于代码重用和分类)的概念,以及如何使用Ruby的元编程能力来动态定义和修改类和方法。 此外,书中还会涵盖Ruby的异常处理机制,这对于编写健壮的代码至关重要。异常处理...

    Ruby编程Ruby Programming

    根据提供的文件信息,我们将深入探讨与“Ruby编程Ruby Programming”这一主题相关的几个核心知识点。这本面向初学者和高级读者的指南旨在全面介绍Ruby编程语言的基础及其高级特性,因此我们将从多个角度来解析这些...

    ruby programming

    - **动态性**:Ruby支持动态类型,可以在运行时修改类和对象的行为。 - **元编程能力**:Ruby允许在运行时定义方法和修改代码结构。 - **可扩展性**:Ruby可以通过C语言编写扩展模块来增强功能。 - **强大的标准库**...

    Ruby.Programming_向Ruby之父学程序设计(第2版)

    另外,若您不是初学者,但想要从头开始学习Ruby语言,这《Ruby Programming:向Ruby之父学程序设计(第2版)》也会派上用场。Ruby是为了让程序设计更快乐而开发的程序语言。Ruby具有“彻底面向对象”、“丰富的程序库”...

    Ruby.Programming_向Ruby之父学程序设计(第2版).pdf (含书签)

    [Ruby.Programming_向Ruby之父学程序设计(第2版)].(日)高桥征义,(日)后藤裕藏.扫描版(ED2000.COM).pdf ) 带书签

    ruby笔记2ruby笔记2ruby笔记2

    "ruby笔记2ruby笔记2ruby笔记2"可能是指一系列关于Ruby学习的笔记,这些笔记可能涵盖了Ruby的基础概念、核心特性以及进阶话题。在Ruby的学习过程中,理解和掌握以下几个关键知识点至关重要: 1. **面向对象编程...

    ruby笔记3ruby笔记3ruby笔记3

    在Ruby笔记3中,我们将会深入探讨这个强大的语言的各个方面,包括基础语法、类与对象、模块、方法、控制结构、异常处理、文件操作以及一些高级特性。 首先,让我们从基础语法开始。Ruby中的变量分为四种类型:局部...

Global site tag (gtag.js) - Google Analytics