Rust 中的繼承與代碼復用


在學習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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM