Advertisement
konalisp

class2

Dec 12th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.95 KB | None | 0 0
  1. fn main() {
  2.     let mut monster: MammalType = Mob::new();
  3.     println!("monster's HP is {} and Hunger is {}", monster.hp, monster.hunger);
  4.     monster.hunger = 15;
  5.     monster.chkhunger();
  6.     println!("monster's HP is {} and Hunger is {}", monster.hp, monster.hunger);
  7.     monster.sodoku();
  8.     println!("monster committed sodoku and HP is now {}", monster.hp);
  9. }
  10.  
  11. struct MammalType {
  12.     hp: int,
  13.     hunger: int
  14. }
  15.  
  16. trait Mammal {
  17.     fn new() -> Self;
  18.     fn chkhunger(&mut self);
  19. }
  20.  
  21. impl Mammal for MammalType {
  22.     fn new() -> MammalType {
  23.         MammalType { hp: 10, hunger: 0 }
  24.     }
  25.     fn chkhunger(&mut self) {
  26.         if self.hunger > 10 {
  27.             self.hp -= 1;
  28.         }
  29.     }
  30. }
  31.  
  32. trait Mob : Mammal {
  33.     fn new() -> Self;
  34.     fn sodoku(&mut self);
  35. }
  36.  
  37. impl Mob for MammalType {
  38.     fn new() -> MammalType {
  39.         MammalType { hp: 10, hunger: 0 }
  40.     }
  41.     fn sodoku(&mut self) {
  42.         self.hp = 0;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement