Advertisement
ZivkicaI

AbstractDataTypesAndTraits

Nov 27th, 2019
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.78 KB | None | 0 0
  1. package exercises.part2oop
  2.  
  3. object AbstractDataTypes extends App {
  4.  
  5.   //abstract (zoshto nekogash nekoe pole kje treba da bide prazno
  6.  
  7.   abstract class Animal{
  8.     val tip:String
  9.     def eat:Unit
  10.  
  11.   }
  12.  
  13.   //val animal=new Animal NE MOZE
  14.  
  15.   class Dog extends Animal {
  16.     override val tip:String="Canine"
  17.     def eat: Unit = println("crunch crunch")
  18.   }
  19.  
  20.   //TRAITS
  21.  
  22.   trait Carnivore{
  23.     def eat(animal:Animal):Unit
  24.  
  25.   }
  26.  
  27.   trait ColdBlooded
  28.   class Crocodile extends Animal with Carnivore with ColdBlooded {   //moze i povekje od edna
  29.     val tip:String="crock"
  30.     def eat:Unit=println("nomnomnom")
  31.     def eat(animal:Animal):Unit = println(s"I am a crock and i am eating ${animal.tip}")
  32.   }
  33.   val dog=new Dog
  34.   val crock=new Crocodile
  35.   crock.eat(dog)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement