在学习Rust过程中突然想到怎么实现继承,特别是用于代码复用的继承,于是在网上查了查,发现不是那么简单的。
C++的继承 ******
首先看看c++中是如何做的。
例如要做一个场景结点的Node类和一个Sprite类继承它。
定义一个node基类
struct Node {
float x;
float y;
void move_to(float x, float y) {
this->x = x;
this->y = y;
}
virtual void draw() const {
printf("node: x = %f, y = %f\n", x, y);
}
};
再定义一个子类Sprite,重载draw方法:
struct Sprite: public Node {
virtual void draw() const {
printf("sprite: x = %f, y = %f\n", x, y);
}
};
可以把 sprite作为一个 Node来使用,并且可以重用 Node中的move_to函数:******
Node* sprite = new Sprite();
sprite->move_to(10, 10);
sprite->draw();
-----------------------------------------------------------
本文转载后的增加::
输出结果:
sprite: x = 10.000000, y = 10.000000
说明 1. 使用了 node基类的 move_to(float x, float y),子类Sprite 的draw();
注意区别:
Node* abc= new Node(); //也可以不要node后的括号:Node* abc= new Node;
abc->move_to(20, 20);
abc->draw();
输出结果:
node: x = 20.000000, y = 20.000000
-------------
说明 2. 在C++中,struct 与 class 的区别
https://blog.csdn.net/alidada_blog/article/details/83419757
在C++中我们可以看到struct和class的区别并不是很大,两者之间有很大的相似性。那么为什么还要保留struct,这是因为C++是向下兼容的,因此C++中保留了很多C的东西。
一.首先看一下C中struct
1.struct的定义
struct A
{
int a;
int b;
//成员列表
};
注意:因为struct是一种数据类型,那么就肯定不能定义函数,所以在面向c的过程中,struct不能包含任何函数。否则编译器会报错。
面向过程的编程认为,数据和数据操作是分开的。然而当struct进入面向对象的c++时,其特性也有了新发展,就拿上面的错误函数来说,在c++中就能运行,因为在c++中认为数据和数据对象是一个整体,不应该分开,这就是struct在c和c++两个时代的差别。
在C++中struct得到了很大的扩充:
1.struct可以包括成员函数
2.struct可以实现继承
3.struct可以实现多态
struct 与 class 的区别:
1.默认的继承访问权。class默认的是private,strcut默认的是public。
2.默认访问权限:struct作为数据结构的实现体,它默认的数据访问控制是public的,而class作为对象的实现体,它默认的成员变量访问控制是private的。
3.“class”这个关键字还用于定义模板参数,就像“typename”。但关建字“struct”不用于定义模板参数
4.class和struct在使用大括号{ }上的区别
关于使用大括号初始化
1.)class和struct如果定义了构造函数的话,都不能用大括号进行初始化
2.)如果没有定义构造函数,struct可以用大括号初始化。
3.)如果没有定义构造函数,且所有成员变量全是public的话,class可以用大括号初始化
====================================
Rust中的继承
现在要用Rust做同样的事。定义一个Node基类:
struct Node {
x: f32,
y: f32,
}
impl Node {
fn draw(&self) {
println!("node: x={}, y={}", self.x, self.y)
}
fn move_to(&mut self, x: f32, y: f32) {
self.x = x;
self.y = y;
}
}
定义子类的时候我们遇到了麻烦:Rust里struct是不能继承的!
struct Sprite: Node;
这么写会报错:
error: `virtual` structs have been removed from the language
virtual struct是什么东西?原来Rust曾经有一个virtual struct的特性可以使struct继承另一个struct,但是被删掉了:(
RFC在这里。现在Rust的struct是不能继承的了。
使用 trait
Rust 里的 trait 是类似于 java 里 interface,可以继承的。我们把 Node 定义为 trait。
trait Node {
fn move_to(&mut self, x: f32, y: f32);
fn draw(&self);
}
但我们发现没有办法在 Node 中实现 move_to 方法,因为 trait 中不能有成员数据:x, y。
那只好在每个子类中写各自的方法实现,例如我们需要一个空Node类和一个Sprite类:
struct EmptyNode {
x: f32,
y: f32,
}
impl Node for EmptyNode {
fn draw(&self) {
println!("node: x={}, y={}", self.x, self.y)
}
fn move_to(&mut self, x: f32, y: f32) {
self.x = x;
self.y = y;
}
}
struct Sprite {
x: f32,
y: f32,
}
impl Node for Sprite {
fn draw(&self) {
println!("sprite: x={}, y={}", self.x, self.y)
}
fn move_to(&mut self, x: f32, y: f32) {
self.x = x;
self.y = y;
}
}
是不是觉得有大量代码重复了?Sprite只需要重写 draw方法,但要把所有方法都实现一遍。如果要实现很多种 Node,每种都要实现一遍,那就要写吐血了。
组合
组合是一个代码重用的好方法。要重用代码时,组合而且比继承更能体现“has-a”的关系。我们把 Node 重新定义为之前的 struct 基类,然后把 Node 放在 Sprite 中:
struct Node {
x: f32,
y: f32,
}
impl Node {
fn draw(&self) {
println!("node: x={}, y={}", self.x, self.y)
}
fn move_to(&mut self, x: f32, y: f32) {
self.x = x;
self.y = y;
}
}
struct Sprite {
node: Node
}
impl Sprite {
fn draw(&self) {
println!("sprite: x={}, y={}", self.node.x, self.node.y)
}
fn move_to(&mut self, x: f32, y: f32) {
self.node.move_to(x, y);
}
}
清爽了不少,美中不足的是还不能省略 move_to 方法,还要手动写一遍,简单调用 Node 中的同名方法。
组合和继承还有一些不同的,比如不能把 Sprite 转型为 Node。
Deref & DerefMut trait
std::ops::Deref 用于重载取值运算符: *。这个重载可以返回其他类型,正好可以解决组合中不能转换类型的问题。
在这个例子中,由于 move_to 的 self 可变的,所以要实现 Deref 和 DerefMut
struct Sprite {
node: Node
}
impl Sprite {
fn draw(&self) {
println!("sprite: x={}, y={}", self.node.x, self.node.y)
}
}
impl Deref for Sprite {
type Target = Node;
fn deref<'a>(&'a self) -> &'a Node {
&self.node
}
}
impl DerefMut for Sprite {
fn deref_mut<'a>(&'a mut self) -> &'a mut Node {
&mut self.node
}
}
之后就可以把 &Sprite 转换为 &Node
let mut sprite = Sprite{ node: Node { x: 10.0, y: 20.0 } };
let mut sprite_node: &mut Node = &mut sprite;
sprite_node.move_to(100.0, 100.0);
要注意的是对sprite_node的方法调用重载是不起作用的。如果 sprite_node.draw(),调用的还是Node.draw(),而不是Sprite.draw()。
如果要调用子类的方法,必须有子类类型的变量来调用。
let mut sprite = Sprite{ node: Node { x: 10.0, y: 20.0 } };
// 这个大括号限制 mut borrow 范围
{
let mut sprite_node: &mut Node = &mut sprite;
sprite_node.move_to(100.0, 100.0);
sprite.node.draw(); // 输出 node: x=100, y=100
}
sprite.draw(); // 输出 sprite: x=100, y=100