Advertisement
eranseg

Animals

Mar 18th, 2020
2,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.66 KB | None | 0 0
  1. typealias animal = (type : String, name : String, numOfLegs : Int, voice : String, live : Bool)
  2.  
  3. class Lion {
  4.    
  5.     //Instance variables
  6.     private var _name : String
  7.     private var _numOfLegs : Int
  8.     private var _voice : String
  9.     private var _live : Bool
  10.    
  11.     //c'tor
  12.     init(name : String) {
  13.         self._name = name
  14.         self._numOfLegs = 4
  15.         self._voice = "wrrrroooorrrr"
  16.         self._live = true
  17.     }
  18.    
  19.     func eat(food : animal) {
  20.         if food.type != "cow" {
  21.             print("Can eat only cows")
  22.         } else {
  23.             print("Yammi")
  24.         }
  25.     }
  26.     func makeSound() {
  27.         if self._live == true {
  28.             print(self._voice)
  29.         }
  30.     }
  31. }
  32.  
  33. class Cow {
  34.    
  35.     //Instance variables
  36.     private var _name : String
  37.     private var _numOfLegs : Int
  38.     private var _voice : String
  39.     private var _live : Bool
  40.    
  41.     //c'tor
  42.     init(name : String) {
  43.         self._name = name
  44.         self._numOfLegs = 4
  45.         self._voice = "mooooo"
  46.         self._live = true
  47.     }
  48.     func makeSound() {
  49.         if self._live == true {
  50.             print(self._voice)
  51.         }
  52.     }
  53. }
  54.  
  55. class Cat {
  56.    
  57.     //Instance variables
  58.     private var _name : String
  59.     private var _numOfLegs : Int
  60.     private var _voice : String
  61.     private var _live : Bool
  62.    
  63.     //c'tor
  64.     init(name : String) {
  65.         self._name = name
  66.         self._numOfLegs = 4
  67.         self._voice = "miaaaooooo"
  68.         self._live = true
  69.     }
  70.    
  71.     func eat(food : animal) {
  72.         if food.type != "Mouse" {
  73.             print("Can eat only mice")
  74.         } else {
  75.             print("Yammi")
  76.         }
  77.     }
  78.    
  79.     func drink(food : animal) {
  80.         if food.type != "Cow" && food.live == true {
  81.             print("Can drink only from cows")
  82.         }
  83.     }
  84.    
  85.     func makeSound() {
  86.         if self._live == true {
  87.             print(self._voice)
  88.         }
  89.     }
  90. }
  91.  
  92. class Mouse {
  93.    
  94.     //Instance variables
  95.     private var _name : String
  96.     private var _numOfLegs : Int
  97.     private var _voice : String
  98.     private var _live : Bool
  99.    
  100.     //c'tor
  101.     init(name : String) {
  102.         self._name = name
  103.         self._numOfLegs = 4
  104.         self._voice = "tsftsftsfstf"
  105.         self._live = true
  106.     }
  107.     func makeSound() {
  108.         if self._live == true {
  109.             print(self._voice)
  110.         }
  111.     }
  112. }
  113.  
  114. var lion = Lion(name : "Mufasa")
  115. var cow : animal = ("cow", "Dolly", 4, "mooooo", true)
  116. var jerry : animal = ("lion", "Jerry", 4, "wrrroorrr", true)
  117. var mitsi = Cat(name : "Mitsi")
  118.  
  119. lion.eat(food: cow)
  120. mitsi.eat(food: cow)
  121. mitsi.drink(food: cow)
  122. mitsi.makeSound()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement