第二章

类继承,属性,类变量

1.如何声明一个子类

class Treasure < Thing

这样Thing类中的属性name,description都被Treasure继承

2.以下三种方式传入父类initialize方法的参数分别是什么?

# This passes a, b, c to the superclass

def initialize( a, b, c, d, e, f )
  super( a, b, c )
end # This passes a, b, c to the superclass def initialize( a, b, c )
  super
end # This passes no arguments to the superclass def initialize( a, b, c)
  super()
end

第一种把参数中a,b,c传入父类initialize方法;第二种传入全部参数,但不加上括号;第三种不传入参数

3.属性的setter/getter

有人这样写setter/getter:

puts( t1.get_description ) 
t1.set_description( “Some description” )

这样似乎更方便一些:

puts( t1.description )
t1.description = “Some description”

如果你想用第二种写法,你不得不这么写:

def description
return @description
end def description=( aDescription )
@description = aDescription
end

注:这是正确的:def name=(aName)

  但这却是错的:def name  =(aName)

你看出差别的了吗?

根据上一章,你可以知道,这里定义了两个方法:description方法和description=方法。原来是通过将"="加入方法名实现的,ruby真是神奇,Java就不能这样写。

然而,事实上有更简单的方法来实现setter/getter

attr_reader :description
attr_writer :description

由一个attr_reader/attr_writer加上符号(:description)构成,事实上,可以一次为多个元素设置setter/getter

attr_writer(:name, :description) 
attr_accessor(:value, :id, :owner)

attr_accessor

等价于:

attr_reader :value

attr_writer :value

4.super

和Java不一样,Ruby中的super方法可以出现在任何方法中,而不只是initialize(构造方法)中。

在第2点中就对super方法的使用有介绍,单独的super将所有参数传给父类initialize,而带参数的super则将指定参数传给父类initialize。

# This passes aName, aDescription to the superclass
def initialize( aName,aDescription )
  super( aName, aDescription )
end # This passes a, b, c to the superclass's aMethod
def aMethod( a, b, c )
  super
end

5.常量和嵌套类(constants & nested class)

class X
A = 10 class Y
def xyz
puts( "goodbye" )
end
end def self.abc
puts("hello")
end
end

常量:以大写字母开头的变量。

如果要访问常量或内部类,需用 ::

puts( X::A )
X::abc # 你也可以用::来调用方法
X.abc # 当然这样也可以 ob = X::Y.new
ob.xyz

6.部分类(Partial Class)

在Ruby中可以对现存的类进行修改,并影响已经生成的对象

class A
def a
puts 'a'
end
end a = A.new
a.public_methods(false)//打印A class所有public的方法
# => [:a] //只有a class A
def b
puts 'b'
end
end a.public_methods(false)
# => [:a, :b]//有a和b

而不能修改的,是类继承了哪个类。比如

class A
def a
puts 'a'
end
end class A < String
def c
puts 'c'
end
end
# TypeError: superclass mismatch for class A
# 所有类默认继承了Object类,A也继承了Object,所以当你让A继承String时就会报错

Ruby学习-第二章的更多相关文章

  1. oracle学习 第二章 限制性查询和数据的排序 ——03

    这里.我们接着上一小节2.6留下的问题:假设要查询的字符串中含有"_"或"%".又该如何处理呢? 開始今天的学习. 2.7  怎样使用转义(escape)操作符 ...

  2. Asp.Net MVC4 + Oracle + EasyUI 学习 第二章

    Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...

  3. Java基础知识二次学习-- 第二章 基础语法与递归补充

    第二章 基础语法与递归补充   时间:2017年4月24日10:39:18 章节:02章_01节,02章_02节 视频长度:49:21 + 15:45 内容:标识符,关键字与数据类型 心得:由字母,下 ...

  4. C#高级编程 (第六版) 学习 第二章:C#基础

    第二章 基础 1,helloworld示例: helloworld.cs using System; using System.Collections.Generic; using System.Li ...

  5. Struts2框架学习第二章——Struts2下的HelloWorld

    本章要点 —  Struts 2的下载和安装 — 纯手工创建一个Web应用 — 纯手工创建一个Struts 2应用 — 实现Struts 2的Action — 配置Struts 2的Action — ...

  6. Ruby学习-第一章

    第一章 字符串,数字,类和对象 为了证明Ruby真的好用,hello world也能写的如此简洁: puts 'hello world' 1.输入/输出 print('Enter your name' ...

  7. C语言学习第二章

    今天开始学习常量,变量,基本数据类型,printf()函数和scanf()函数,算术运算符. 首先常量:是在程序中保持不变的量 变量:编写程序时,常常需要将数据存储在内存中,方便后面使用这个数据或者修 ...

  8. Python 学习第二章

    本章内容 数据类型 数据运算 表达式 if ...else 语句 表达式 for 循环 表达式 while 循环 一.数据类型 在内存中存储的数据可以有多种类型. 在 Python 有五个标准的数据类 ...

  9. 【转载】Gradle学习 第二章:概述

    转载地址:http://ask.android-studio.org/?/article/6 Here is a list of some of Gradle's features.<翻译> ...

随机推荐

  1. nginx+tomcat+redis完成session共享

    本文记录nginx+redis+tomcat实现session共享的过程 nginx安装:http://blog.csdn.net/grhlove123/article/details/4783467 ...

  2. C#后台发送HTTP请求

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using Syst ...

  3. SAP HANA 开发者中心(Developer Center)入门指南

  4. Request.ServerVariables["Url"]

    Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提 ...

  5. [Swust OJ 767]--将军回家(Dijkstra算法)

    题目链接:http://acm.swust.edu.cn/problem/767/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  6. easyui-datagrid通过action从数据库获取数据的关键代码

    实际上是结合struts2来从数据获取json格式的数据.   关键代码: GetUserAction.java代码   package com.log.control; import java.io ...

  7. jquery 动态增加的html元素,初始化设置在id或class上的事件无效

    一般情况,我们会在页面初始化完成后对class定义一些全局事件,举个栗子: $(document).ready(function(){ $(".class").on("m ...

  8. poj 2187 Beauty Contest 最远点距

    /** 求出凸包枚举每个点的矩距离即可 因为凸包上的点可定不多.. 学习: 刚开始WA 了一次,,因为用int 存的, 一看discuss 里提供的数据,想起来,,应该是越界了.. 后来用longlo ...

  9. cygwin org/apache/zookeeper/KeeperException

    以前用cdh3-0.20的hbase,在windows下面直接启动就行了,但是最近安装0.94以上的,就不行了. 报标题的错误,搜遍网络,几乎都是要加HBASE_CLASSPATH的,后来看老外的文章 ...

  10. 跨平台生成GUID/UUID

    #ifndef XGUID_H#define XGUID_H #include <string>#include <stdio.h>#ifdef WIN32#include & ...