Redclock's Blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在学习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();

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
posted on 2015-11-25 21:19  Redclock  阅读(17343)  评论(2编辑  收藏  举报