Advertisement
mmayoub

first example, animals 25.01.18

Jan 25th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.95 KB | None | 0 0
  1. class Animal
  2. {
  3.     var name:String
  4.     var isAlive:Bool
  5.  
  6.     init(name:String) {
  7.         self.name=name
  8.         self.isAlive=true
  9.     }
  10.  
  11.     func makeSound()
  12.     {
  13.         print("Animal is making a sound ...")
  14.     }
  15. }
  16.  
  17.  
  18. class Lion:Animal
  19. {  
  20.     func eat(cow:Cow) {
  21.         if (self.isAlive)
  22.         {
  23.             if (cow.isAlive)
  24.             {
  25.                 print("Lion \(self.name) is eating cow \(cow.name)")
  26.                 cow.isAlive=false
  27.             }
  28.             else
  29.             {
  30.                 print("Lion \(self.name) cann't eat a dead cow \(cow.name)")
  31.             }
  32.         }
  33.         else
  34.         {
  35.                 print("dead Lion \(self.name) cann't eat a cow \(cow.name)")
  36.         }
  37.        
  38.     }  
  39.    
  40.     override func makeSound()
  41.     {
  42.         print("Lion is making a sound ...")
  43.     }
  44. }
  45.  
  46.  
  47.  
  48. class Cat:Animal
  49. {
  50.     func eat(mouse:Mouse) {
  51.         if (self.isAlive)
  52.         {
  53.             if (mouse.isAlive)
  54.             {
  55.                 print("Cat \(self.name) is eating mouse \(mouse.name)")
  56.                 mouse.isAlive=false
  57.             }
  58.             else
  59.             {
  60.                 print("Cat \(self.name) cann't eat a dead mouse \(mouse.name)")
  61.             }
  62.         }
  63.         else
  64.         {
  65.                 print("Dead Cat \(self.name) cann't eat a mouse \(mouse.name)")
  66.         }
  67.        
  68.     }  
  69.    
  70.     func drinkMilk(cow:Cow) {
  71.         if (self.isAlive)
  72.         {
  73.             if (cow.isAlive)
  74.             {
  75.                 print("Cat \(self.name) is drinking milk from cow \(cow.name)")
  76.             }
  77.             else
  78.             {
  79.                 print("Cat \(self.name) cann't get milk from a dead cow \(cow.name)")
  80.             }
  81.         }
  82.         else
  83.         {
  84.                 print("Dead cat \(self.name) cann't drink milk from cow \(cow.name)")
  85.         }
  86.        
  87.     }  
  88.    
  89.     override func makeSound()
  90.     {
  91.         print("Cat is making a sound ...")
  92.     }
  93. }
  94.  
  95. class Cow:Animal
  96. {
  97.     func canGiveMilk() -> Bool
  98.     {
  99.         return self.isAlive
  100.     }
  101.    
  102.     override func makeSound()
  103.     {
  104.         print("Cow is making a sound ...")
  105.     }
  106. }
  107.  
  108. class Mouse:Animal
  109. {
  110.     override func makeSound()
  111.     {
  112.         print("Mouse is making a sound ...")
  113.     }
  114. }
  115. var lion:Lion=Lion(name:"myLion")
  116. var cow:Cow=Cow(name:"myCow")
  117. var cat:Cat=Cat(name:"myCat")
  118.  
  119. lion.makeSound()
  120. cow.makeSound()
  121. cat.makeSound()
  122.  
  123. cat.drinkMilk(cow:cow)
  124. lion.eat(cow:cow)
  125. lion.eat(cow:cow)
  126. cat.drinkMilk(cow:cow)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement