面向对象编程中的类与对象深入解析
立即解锁
发布时间: 2025-08-22 00:57:36 阅读量: 1 订阅数: 4 


Perl 6编程入门与实践
### 面向对象编程中的类与对象深入解析
#### 1. 对象属性的声明与特性
对象属性的声明方式会影响其在对象创建时的赋值情况。若按特定方式声明属性,将无法使用默认的 `new` 构造函数在对象创建时赋值,需要自定义构造函数或间接修改 `new`。目前不建议尝试这种方式,因为此时对象的操作受限。
默认情况下,对象属性是不可变的,即只读属性。一旦对象创建,就无法修改这些属性。不过,对于某些属性,这种特性是合适的,比如表示人的对象,其姓名和出生日期通常不会改变。但有些属性可能需要更新,甚至频繁更新。这时,可以使用 `is rw` 特性将属性声明为可变的,示例代码如下:
```perl
class Point2D {
has Numeric $.abscissa is rw; # "x" value
has Numeric $.ordinate is rw; # "y" value
}
```
下面展示如何修改这些属性:
```perl
# First creating a Point2D object:
my $point = Point2D.new(abscissa => 3, ordinate => 4);
say $point; # -> Point2D.new(abscissa => 3, ordinate => 4)
# Now moving the $point object two units to the right:
$point.abscissa = 5;
say $point; # -> Point2D.new(abscissa => 5, ordinate => 4)
```
属性可分为实例属性和类属性。前面提到的大多是实例属性,即与单个对象相关的属性。类属性则与整个类相关,使用 `my` 声明符声明,相对实例属性较少见。例如,类级别的计数器可用于跟踪已实例化的对象数量。
#### 2. 方法的创建
简单的 `Point2D` 类及其实例目前功能有限,可通过添加方法来完善类的定义:
```perl
class Point2D {
has Numeric $.abscissa;
has Numeric $.ordinate;
method coordinates { # accessor to both x and y
return (self.abscissa, self.ordinate)
}
method distanceToCenter {
(self.abscissa ** 2 + self.ordinate ** 2) ** 0.5
}
method polarCoordinates {
my $radius = self.distanceToCenter;
my $theta = atan2 self.ordinate, self.abscissa;
return $radius, $theta;
}
}
```
上述类中声明了三个方法:
- `coordinates`:用于获取笛卡尔坐标的简单访问器。
- `distanceToCenter`:计算并返回对象与原点之间的距离。
- `polarCoordinates`:计算对象在极坐标系统中的半径和方位角(`$theta`),该方法调用了 `distanceToCenter` 方法来获取极坐标的半径分量。
方法定义与子程序定义类似,但使用 `method` 关键字而非 `sub` 关键字。方法本质上是在类(或角色)中定义的子程序,它知道调用它的对象及其类,并且调用语法不同。
子程序和方法的另一个重要区别在于,不同类(或角色)中可能定义了多个同名方法,方法调用涉及调度阶段,对象系统通常根据调用者的类或类型选择要调用的方法。不过在 Perl 6 中,由于存在多子程序(即同名但签名不同的子程序,在运行时根据参数数量和类型解析),这种区别变得模糊。
在方法定义中,`self` 指调用该方法的对象,也可用 `$.` 作为简写,例如 `coordinates` 方法可改写为:
```perl
method coordinates { # accessor to both x and y
return ($.abscissa, $.ordinate)
}
```
`$.` 和 `self` 这两种语法格式基本等效。还有第三种语法,使用感叹号代替点号:
```perl
method coordinates { # accessor to both x and y
return ($!abscissa, $!ordinate)
}
```
虽然结果相同,但这种新语法并不等效。`$.abscissa` 是方法调用,而 `$!abscissa` 是直接访问属性。`$!abscissa` 仅在类内部可用,可能速度稍快,而方法调用语法可在其他地方使用。
下面创建对象并调用这些方法:
```perl
my $point = Point2D.new(
abscissa => 4,
ordinate => 3
);
say $point.coordinates; # -> (4 3)
say $point.distanceToCenter; # -> 5
say $point.polarCoordinates; # -> (5 0.643501108793284)
```
若不指定显式调用者使用方法,该方法将应用于 `$_` 主题变量。例如:
```perl
.say for <one two three>; # -> one two three (each on one line)
```
对于创建的带有方法的对象,也可利用此语法捷径。例如:
```perl
given $point {
say .coordinates; # -> (4 3)
say .distanceToCenter; # -> 5
.polarCoordinates.say; # -> (5 0.643501108793284)
}
```
作为练习,可以编写一个名为 `distance_between_points` 的方法,该方法接受两个点作为参数,并使用勾股定理返回它们之间的距离。
目前类中的方法都是访问器,用于提供调用者某些属性的快照。若属性是可变的(使用 `is rw` 特性声明),还可创建修改器,即用于修改这些可变属性的方法,示例如下:
```perl
class Point2D-mutable {
has Numeric $.abscissa is rw;
has Numeric $.ordinate is rw;
# perhaps the same accessors as in the class definition above
method new-ordinate (Numeric $ord) {
self.ordinate = $ord;
}
}
# Creating the Point2D-mutable object:
my $point = Point2D-mutable.new(abscissa => 3, ordinate => 4);
say $point; # -> Point2D-mutable.new(abscissa => 3, ordinate => 4)
# Modifying the ordinate:
$point.new-ordinate(6);
say $point; # -> Point2D-mutable.new(abscissa => 3, ordinate => 6)
```
#### 3. 矩形类与对象组合
设计表示矩形的类时,需要决定使用哪些属性来指定矩形的位置和大小。假设矩形的边是垂直或水平的,忽略角度,有至少两种方案:
- 指定矩形的一个角(或中心)、宽度和高度。
- 指定两个相对的角。
这里以第一种方案为例,类定义如下:
```perl
class Rectangle {
has Numeric $.width;
has Numeric $.height;
has Point2D $.corner; # lower left vertex
method area { return $.width * $.height }
method topLeft { $.corner.abscissa, $.corner.ordinate + $.height; }
# other methods, e.g. for other corners' coordinates, center, etc.
}
```
与之前的 `Point2D` 类定义相比,`Rectangle` 类使用了之前创建的 `Point2D` 类型来定义 `corner` 属性。
`topLeft` 方法返回矩形左上角的坐标,这个方法有助于解释 `$.` 和 `$!` 符号的区别。使用 `$.corner.abscissa` 是调用访问器,也可直接访问 `R
0
0
复制全文
相关推荐










