Ruby面向对象编程核心概念与实践
立即解锁
发布时间: 2025-08-20 01:30:31 阅读量: 1 订阅数: 3 


Ruby编程入门与实践指南
# Ruby 面向对象编程核心概念与实践
## 1. 为何采用面向对象编程
在软件开发领域,存在面向对象和过程式两种编程风格。为了更直观地展示二者差异,假设有两位开发者接到相同任务:编写计算不同形状(正方形和三角形)周长与面积的代码。
### 1.1 过程式编程实现
过程式程序员迅速完成代码编写,实现了四个函数:
```ruby
def perimeter_of_square(side_length)
side_length * 4
end
def area_of_square(side_length)
side_length * side_length
end
def perimeter_of_triangle(side1, side2, side3)
side1 + side2 + side3
end
def area_of_triangle(base_width, height)
base_width * height / 2
end
```
### 1.2 面向对象编程实现
面向对象程序员则花费更多时间,定义了 `Shape` 类,并创建了继承自 `Shape` 的 `Square` 和 `Triangle` 类:
```ruby
class Shape
end
class Square < Shape
def initialize(side_length)
@side_length = side_length
end
def area
@side_length * @side_length
end
def perimeter
@side_length * 4
end
end
class Triangle < Shape
def initialize(base_width, height, side1, side2, side3)
@base_width = base_width
@height = height
@side1 = side1
@side2 = side2
@side3 = side3
end
def area
@base_width * @height / 2
end
def perimeter
@side1 + @side2 + @side3
end
end
```
### 1.3 对比分析
过程式代码简洁,但当需求变化或处理大量不同形状时,开发者需牢记每种形状对应的计算函数。而面向对象代码虽初始编写复杂,但具有更好的可扩展性和可维护性。例如,后续添加新形状时,只需继承 `Shape` 类并实现相应方法即可。
### 1.4 结论
面向对象编程的优势在于,即使代码结构更复杂,但非专业人员也能轻松理解类和对象之间的关系,且代码更易于维护和更新,以适应实际应用场景。
## 2. 面向对象编程基础
### 2.1 类与对象
- **类**:是对象的蓝图,定义了对象的属性和方法。例如 `Shape` 类可用于创建多个形状对象。
- **对象**:是类的实例。通过 `Shape.new` 可创建 `Shape` 类的新实例。
### 2.2 变量类型
#### 2.2.1 局部变量
局部变量只能在定义它的代码块内使用。示例如下:
```ruby
x = 10
def basic_method
puts x
end
basic_method
```
运行上述代码会报错,因为 `x` 是局部变量,在 `basic_method` 方法中不可用。
#### 2.2.2 全局变量
全局变量以 `$` 开头,可在程序的任何地方使用。示例:
```ruby
def basic_method
puts $x
end
$x = 10
basic_method
```
#### 2.2.3 实例变量
实例变量以 `@` 开头,属于特定对象。在 `Square` 类中,`@side_length` 就是实例变量,可在对象的不同方法中使用。示例:
```ruby
class Square
def initialize(side_length)
@side_length = side_length
end
def area
@side_length * @side_length
end
end
a = Square.new(10)
puts a.area
```
#### 2.2.4 类变量
类变量以 `@@` 开头,作用域为整个类。可用于存储与类的所有对象相关的信息,如记录创建的对象数量:
```ruby
class Square
def initialize
if defined?(@@number_of_squares)
@@number_of_squares += 1
else
@@number_of_squares = 1
end
end
end
```
### 2.3 类方法与对象方法
- **对象方法**:操作特定对象,如 `Square` 类的 `area` 方法。
- **类方法**:直接作用于类本身。示例:
```ruby
class Square
def self.test_method
puts "Hello from the Square class!"
end
def test_method
puts "Hello from an instance of class Square!"
end
end
Square.test_method
Square.new.test_method
```
### 2.4 继承
继承是面向对象编程的重要概念,允许子类继承父类的属性和方法。Ruby 只支持单继承。示例:
```ruby
class ParentClass
def method1
puts "Hello from method1 in the parent class"
end
def method2
puts "Hello from method2 in the parent class"
end
end
class ChildClass < ParentClass
def method2
puts "Hello from method2 in the child class"
end
end
my_object = ChildClass.new
my_object.method1
my_object.method2
```
继承的好处是子类可复用父类的功能,并添加自己的特定功能。
### 2.5 方法重写
Ruby 作为动态语言,允许重写现有类和方法。例如重写 `String` 类的 `length` 方法:
```ruby
class String
def length
20
end
end
puts "This is a test".length
```
重写方法时需谨慎,以免影响程序的正常运行。
### 2.6 反射与对象方法查询
反射允许程序在运行时检查、分析和修改自身。可使用 `methods` 方法查询对象的所有可用方法:
```ruby
a = "This is a test"
puts a.methods.join(' ')
```
### 2.7 封装
封装可控制类的方法和属性的可见性,分为公共、私有和受保护三种级别。示例:
```ruby
class Person
def initialize(name)
set_name(name)
end
def name
@first_name + ' ' + @last_name
end
private
def set_name(name)
first_name, last_name = name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name(name)
@first_name = name
end
def set_last_name(name)
@last_name = name
end
end
```
### 2.8 多态
多态允许代码同时处理不同类型和类的对象。例如 `+` 方法可用于数字相加、字符串连接和数组合并。示例:
```ruby
class Animal
attr_accessor :name
def initialize(name)
@name = name
end
end
class Cat < Animal
def talk
"Meaow!"
end
end
class Dog < Animal
def talk
"Woof!"
end
end
animals = [Cat.new("Flossie"), Dog.new("Fido"), Cat.new("Tinkle")]
animals.each do |animal|
puts animal.talk
end
```
### 2.9 嵌套类
在 Ruby 中,可在类中定义其他类,称为嵌套类。示例:
```ruby
class Drawing
class Line
end
class Ci
```
0
0
复制全文
相关推荐









