Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Animal
- {
- var name:String
- var isAlive:Bool
- init(name:String) {
- self.name=name
- self.isAlive=true
- }
- func makeSound()
- {
- print("Animal is making a sound ...")
- }
- }
- class Lion:Animal
- {
- func eat(cow:Cow) {
- if (self.isAlive)
- {
- if (cow.isAlive)
- {
- print("Lion \(self.name) is eating cow \(cow.name)")
- cow.isAlive=false
- }
- else
- {
- print("Lion \(self.name) cann't eat a dead cow \(cow.name)")
- }
- }
- else
- {
- print("dead Lion \(self.name) cann't eat a cow \(cow.name)")
- }
- }
- override func makeSound()
- {
- print("Lion is making a sound ...")
- }
- }
- class Cat:Animal
- {
- func eat(mouse:Mouse) {
- if (self.isAlive)
- {
- if (mouse.isAlive)
- {
- print("Cat \(self.name) is eating mouse \(mouse.name)")
- mouse.isAlive=false
- }
- else
- {
- print("Cat \(self.name) cann't eat a dead mouse \(mouse.name)")
- }
- }
- else
- {
- print("Dead Cat \(self.name) cann't eat a mouse \(mouse.name)")
- }
- }
- func drinkMilk(cow:Cow) {
- if (self.isAlive)
- {
- if (cow.isAlive)
- {
- print("Cat \(self.name) is drinking milk from cow \(cow.name)")
- }
- else
- {
- print("Cat \(self.name) cann't get milk from a dead cow \(cow.name)")
- }
- }
- else
- {
- print("Dead cat \(self.name) cann't drink milk from cow \(cow.name)")
- }
- }
- override func makeSound()
- {
- print("Cat is making a sound ...")
- }
- }
- class Cow:Animal
- {
- func canGiveMilk() -> Bool
- {
- return self.isAlive
- }
- override func makeSound()
- {
- print("Cow is making a sound ...")
- }
- }
- class Mouse:Animal
- {
- override func makeSound()
- {
- print("Mouse is making a sound ...")
- }
- }
- var lion:Lion=Lion(name:"myLion")
- var cow:Cow=Cow(name:"myCow")
- var cat:Cat=Cat(name:"myCat")
- lion.makeSound()
- cow.makeSound()
- cat.makeSound()
- cat.drinkMilk(cow:cow)
- lion.eat(cow:cow)
- lion.eat(cow:cow)
- cat.drinkMilk(cow:cow)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement