Created
June 17, 2013 00:57
-
-
Save luqmana/5794062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Point { | |
x: int, | |
y: int | |
} | |
trait Positioned { | |
fn x(&self)-> int; | |
fn y(&self)-> int; | |
fn setX(&mut self, int); | |
fn setY(&mut self, int); | |
} | |
trait Movable: Positioned { | |
fn translate(&mut self, dx: int, dy: int); | |
} | |
struct Entity { | |
pos: Point, | |
} | |
impl Positioned for Entity { | |
fn x(&self)-> int { | |
self.pos.x | |
} | |
fn y(&self)-> int { | |
self.pos.y | |
} | |
fn setX(&mut self, v: int) { | |
self.pos.x = v; | |
} | |
fn setY(&mut self, v: int) { | |
self.pos.y = v; | |
} | |
} | |
impl Movable for Entity { | |
fn translate(&mut self, dx: int, dy: int) { | |
let x = self.x(); | |
let y = self.y(); | |
self.setX(x + dx); | |
self.setY(y + dy); | |
} | |
} | |
fn main() { | |
let mut e = Entity { | |
pos: Point { x: 0, y: 0 }, | |
}; | |
println(fmt!("%?, %?", e.x(), e.y())); | |
e.translate(5, 10); | |
println(fmt!("%?, %?", e.x(), e.y())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment