From c8d308a566c9e3e5be85e0e67082a06c3420a72b Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 22 Feb 2018 04:32:51 -0600 Subject: -got now a trait which resembles polymorphism for new objects --- src/ship.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/ship.rs') diff --git a/src/ship.rs b/src/ship.rs index f791809..715f019 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,14 +1,33 @@ +use mass::Mass; +extern crate serde_json; + #[derive(Serialize, Deserialize, Debug)] pub struct Ship { - pub name : String, - pub location : (isize, isize, isize), + name : String, + location : (isize, isize, isize), } -impl Ship { - pub fn new(name : &str) -> Ship { +impl Mass for Ship { + fn new(name : &str, location : (isize, isize, isize)) -> Ship { Ship { name : String::from(name), - location : (0,0,0), + location : location, } } + + fn get_name(&self) -> &String { + &self.name + } + + fn get_location(&self) -> (isize, isize, isize) { + self.location + } + + fn give_location(&mut self, location : (isize, isize, isize)) { + self.location = location; + } + + fn serialize(&self) ->String { + serde_json::to_string(self).unwrap() + } } -- cgit v1.2.3