在学习对象之前,让我们先了解 Ruby 中的类。
Ruby 类
类就像创建对象的蓝图。
我们可以将类比作房子的草图(原型)。它包含了地板、门、窗户等所有细节。
根据这些描述,我们建造房子(在这里,房子就是对象)。
因为可以根据同一个描述建造许多房子,所以我们可以从一个类创建许多对象。
在 Ruby 中定义类
我们使用 class
关键字在 Ruby 中创建类。例如,
class ClassName
# Class definition
end
在这里,我们创建了一个名为 ClassName
的类。所有定义类的代码都放在 class
关键字和 end
关键字之间。
注意:类名应使用PascalCase(驼峰命名法),即每个单词的首字母都大写。
示例
class Dog
def bark
puts "Woof!"
end
end
这里,
Dog
- 类的名称。bark
- 类中的一个方法,用于打印字符串"Woof!"
。
Ruby 对象
对象被称为类的实例。您可以通过在类名后使用 .new
来创建对象,如下所示
object_name = ClassName.new
示例
class Dog
def bark
puts "Woof!"
end
end
# Create objects of the class
dog1 = Dog.new
dog2 = Dog.new
在这里,dog1
和 dog2
是 Dog
类的对象(实例)。现在,我们可以使用这个对象来访问类中定义的方法。
注意:在 Ruby 中,对象名和方法名应使用snake_case(蛇形命名法),例如 dog1
、bark
、wag_tail
等。
使用对象访问方法
创建对象/实例后,您可以使用它来访问类中定义的实例方法。例如,
class Dog
# Instance method
def bark
puts "Woof!"
end
end
# Create an object of the class
dog1 = Dog.new
# Access the 'bark' method using dog1 object
dog1.bark
输出
Woof!
在这里,我们创建了一个名为 dog1
的对象。
正如您所见,我们使用代码 dog1.bark
访问了 Dog
类中定义的 bark
方法。
注意:这里,bark
被称为实例方法,因为它是在类的实例/对象上调用的。您也可以定义在类本身而不是其实例上调用的类方法。您稍后会学到它们。
Ruby 实例变量
实例变量存储有关对象的信息。它们始终以 @
开头,并且在整个对象中都可用。例如,
class Dog
# Initializer method to initialize instance variables
def initialize
# Create and initialize instance variables
@name = "Maple"
@breed = "Border Collie"
end
# Instance method
def greet
puts "Woof! My name is #{@name} and I'm a #{@breed}."
end
end
dog1 = Dog.new
dog1.greet
输出
Woof! My name is Maple and I'm a Border Collie.
在这里,@name
和 @breed
是 Dog
类的实例变量。
请注意,我们使用了 initialize
方法来创建和初始化实例变量 — 这是执行此任务的首选方式。
接下来让我们学习这个方法。
Ruby initialize 方法
在 Ruby 中,initialize
方法是一个特殊方法,当从类创建新对象时会自动调用。
它在对象首次创建时设置(或“初始化”)对象的实例变量。
注意: initialize
方法不需要手动调用 — 当您使用 .new
创建对象时,它会自动运行。
示例
class Dog
# Initializer method to initialize instance variables
# Takes parameters 'name' and 'breed'
def initialize(name, breed)
# Assign arguments to the instance variables
@name = name
@breed = breed
end
# Instance method
def greet
puts "Woof! My name is #{@name} and I'm a #{@breed}."
end
end
# Create an object
# Pass "Honey" and "Wolfdog" as arguments to initialize
dog1 = Dog.new("Honey", "Wolfdog")
dog1.greet
# Create another object and pass another set of arguments
dog2 = Dog.new("Maple", "Border Collie")
dog2.greet
输出
Woof! My name is Honey and I'm a Wolfdog. Woof! My name is Maple and I'm a Border Collie.
在这里,initialize 方法接受两个 参数,这些参数存储在 name
和 breed
参数中。
然后将这些参数的值赋给实例变量 @name
和 @breed
。
注意:这里,name
和 @name
是不同的变量。breed
和 @breed
也是如此。
向 initialize 方法传递参数
我们通过创建对象时传递参数给 initialize,如下所示
# Pass "Honey" and "Wolfdog" as arguments
dog1 = Dog.new("Honey", "Wolfdog")
# Pass "Maple" and "Border Collie" as arguments
dog2 = Dog.new("Maple", "Border Collie")
示例:Ruby 类和对象
# Define a Circle class
class Circle
def initialize(radius)
@radius = radius
end
# Instance method that returns area of circle
def calculate_area
3.14 * @radius * @radius
end
end
# Create a circle object with radius 7
circle = Circle.new(7)
# Call the instance method and store its value in a variable
circle_area = circle.calculate_area
# Print the area of the circle
puts "Area of the Circle: #{circle_area}"
输出
Area of the Circle: 153.86
在上面的示例中,我们定义了一个名为 Circle
的类,其中包含以下组件
@radius
- 一个实例变量,用于存储圆的半径。initialize
- 一个初始化方法,用于创建和初始化实例变量。calculate_area
- 一个实例方法,用于返回圆的面积。
然后,我们创建了一个名为 circle
的对象,并将其初始化器传递了7作为参数。
# Create a circle object with radius 7
circle = Circle.new(7)
然后将此值赋给 @radius
实例变量。
最后,我们使用 .
符号访问了它的 calculate_area
方法
# Call the instance method and store its value in a variable
circle_area = circle.calculate_area
常见问题
与实例方法不同,我们不能使用 .
符号直接在类外部访问实例变量。例如,
class Dog
def initialize(name, breed)
@name = name
@breed = breed
end
end
dog1 = Dog.new("Honey", "Wolfdog")
# Invalid: Accessing instance variable 'name'
# NoMethodError: undefined method 'name' for an instance of Dog
puts dog1.name
# Invalid: Accessing instance variable 'breed'
# NoMethodError: undefined method 'breed' for an instance of Dog
puts dog1.breed
有一些间接方法可以在类外部访问实例变量,例如使用 attr_accessor
方法。例如,
class Dog
# Use attr_accessor on instance variables
attr_accessor :name
attr_accessor :breed
def initialize(name, breed)
@name = name
@breed = breed
end
end
dog1 = Dog.new("Honey", "Wolfdog")
# Access instance variables
puts dog1.name
puts dog1.breed
输出
Honey Wolfdog
此程序有效,因为 attr_accessor
会自动为读取和写入这些变量创建 name
和 breed
方法。