定义一个类用 Class ,类名一般以大写开头。每个类都有一个特殊的方法 initialize 这个是类的初始化函数,等同于Java 或C#面的构造函数。
class Book
attr_accessor :title,:content
def initialize(title,content)
@title = title
@content = content
end
end
book = Book.new("world is flat","world ...")
puts book.title
puts book.content
#output:
#world is flat
#world ...
在Ruby中类是永远不会被关闭的,这意味着我们可以在任何时候添加或重写类的方法,当然也可以是变量.
class Book
attr_accessor :author
end
book.author = "danny"
puts book.author
# output
# danny
代码接上的例子,如果我在这里再次定义Book类里的内容,实际上是新增了一个 author的 attribute。
类的继承使用 < 号,子类继承父类的方法和属性,这里我们设计一个卡通书CartoonBook,它将继承它的父类的initialize方法,并且加一个新属性是否彩页。
class CartoonBook < Book
attr_accessor :iscolor
end
book = CartoonBook.new("dargon ball", "all image......")
book.iscolor = true
puts book.title
puts book.iscolor
# output:
# dargon ball
# true
变量的类型有实例变量:Instance Variables , 类变量:Class Variables,变量:Variables.全局变量:Global Variables.第一个例子中我们就用到了instance variable,@title,同时在第一个例子中我们还用到了一个方法attr_accessor,这个方法用来向外部暴露我们的一个实例变量,也就是定义这个类的属性,attr_accessor定义为读写,attr_reader为只读,attr_writer为只写。
类变量在这个类的所有实例中是共享的,ruby中使用@@符号来定义一个类变量。类变量必须先被初始化才能使用,初始化的的工作通常就是在类的定义里为类变量赋值。
class CartoonBook < Book
attr_accessor :iscolor
@@total_rent =0
def initialize(title,content)
super
end
def rent_to(stutdent)
@@total_rent += 1
puts "total rented #@@total_rent books"
end
end
book1 = CartoonBook.new("dargon ball", "all image......")
book1.rent_to("davi")
book2 = CartoonBook.new("micky mouse","all image......")
book2.rent_to("marry")
#output:
# total rented 1 books
# total rented 2 books
上面的例子扩展了CartoonBook这个类,定义了@@total_rent类变量并赋值0,新增了rent_to方法,该方法实现把一本书借个一个学生同时打印书一个借出去了多少卡通书。book1,boo2都是CartoonBook类的实例,book1.rent_to后打印"total rented 1 books"说明@@total_rent==1,当book2.rent_t后@@total_rent==2,这说明了实例变量@@total_rent是在CartoonBook类中的所有实例中是共享一份copy的。
变量:Variables指的是一般变量,它表示对对象的引用,比如上面的book1,book2就是变量,它们表示对CartoonBook对象的引用。name="jack"中name是对String类型对象的引用。
全局变量:Global Variables是在整个程序里都有效的变量。$DEBUG表示现在是否是调试模式,__FILE__当前源文件名,$LOAD_PATH当前环境变量,除了这些还有很多,在书的650页有详细的介绍。
常量:Constants,常量全部用大写字母,在Class和Module中定义的常量调用 Class::CONST,在外面定义的常量
直接调用或 ::CONST.
分享到:
相关推荐
书中强调了这一点,通过示例解释了如何利用类(Class)、对象(Object)以及实例变量(Instance Variables)来创建和操作数据结构。Ruby的面向对象特性使得代码更加模块化,易于维护和扩展。 #### 2. **基本Ruby...
- **第3章:Classes, Objects, and Variables**:深入探讨类、对象和变量的概念与用法。 - **第4章:Containers, Blocks, and Iterators**:讲解容器数据结构、块(block)以及迭代器(iterator)等高级特性。 - **第...
### Ruby元编程基础学习笔记整理 #### 一、语言构建(Language Constructs) 在Ruby中,诸如变量、类和方法等元素统称为语言构建(Language Constructs)。这些构建块是构成Ruby程序的基础。 ##### 示例代码分析 ```...
1. **面向对象编程(Object-Oriented Programming, OOP)**:Ruby是完全的面向对象语言,每个值都是一个对象,包括基本类型如整数、字符串和布尔值。类是创建对象的蓝图,实例化一个类就能创建一个新的对象。理解类...
- **类变量**(Class Variables):以`@@`开头,用于在整个类的实例间共享数据,如`@@count = 0`。 - **全局变量**(Global Variables):以`$`开头,其作用域在整个程序中,如`$debug = true`。 - **常量**...
类常量 目标 了解什么是类常量 使用类常量 知道何时使用attr_accessor VS自己创建方法 描述 之前,我们看到了如何使类的每个单独实例都保留有关其自身的信息。 Book对象( Book类的实例)知道自己的标题,作者和体裁...
- **Class Variables:** Describes class variables, which are shared among all objects of a class and are also prefixed with an `@` symbol. - **Global Variables:** Explains global variables, which can...
类变量和类方法实验室 目标 使用类变量来跟踪与类有关的数据。 定义类方法以公开与类有关的数据。 概述 在本实验中,我们将处理Song类。 Song类可以制作单独的歌曲。 每首歌都有一个名字,一个艺术家和一个流派。...
Variable Scope and Functions Scope and Storage Class Namespaces Functions Summary of Parameter Types Recursion Structured Programming Basics Real-World Programming Programming Exercises Answers to ...
Ruby类变量和类方法实验室目标使用类变量来跟踪与类有关的数据。 定义类方法以公开与类有关的数据。概述在本实验中,我们将处理Song类。 Song类可以制作单独的歌曲。 每首歌都有一个名字,一个艺术家和一个流派。 ...
8. **定义语法糖(Syntax Sugar)**:Ruby提供了许多语法糖,如`attr_accessor`,`class_eval`等,使得元编程更加简洁易读。 9. **动态常量**:虽然Ruby的常量不应在运行时改变,但在某些情况下,如使用`const_set`...
Lesson 31 - Creating a class for an object type Lesson 32 - Working with your own object types Lesson 33 - Customizing classes Lesson 34 - Capstone project: card game UNIT 8 - USING LIBRARIES TO ...
Ruby 支持多种类型的变量,例如局部变量(Local Variables)、实例变量(Instance Variables)、类变量(Class Variables)和全局变量(Global Variables)。变量命名应遵循特定的规则,以确保其有效性和可读性。 #...
类常量的对象定向基础 目标 了解什么是类常量 使用类常量 知道何时使用attr_accessor VS自己创建方法 描述 之前,我们了解了如何使类的每个单独实例都保留有关其自身的信息。 Book对象( Book类的实例)知道自己的...
The book covers different types of variables, including local, instance, class, and global variables, along with constants and parallel assignment. - **Symbols:** Symbols are immutable string-like ...
"Accessing Variables in Block Objects"这个主题主要探讨了如何在Block内部访问外部变量,这在处理异步操作、回调函数或创建自定义闭包时非常常见。以下是对这一知识点的详细说明: 1. **Block定义与类型**: - ...