abstract class Dickmacher {} case class Beverage (name: String, cal: Int) extends Dickmacher case class Food (name: String, cal: Int) extends Dickmacher case class Meal (name: String, food: Food, drink: Beverage) extends Dickmacher case class Combo (starter: Dickmacher, main: Dickmacher) extends Dickmacher //hat wie Food einen val 'cal', erbt aber nicht von Dickmacher class Elephant(val cal: Int) { override def toString() : String = "Elephant" } class Person(name: String) { def eat(food: Food) = { println("Ich habe " + food + " mit " + food.cal + " cal gegessen.") } } val person = new Person("Horst") person.eat(Food("Diatschokolade", 5)) // person.eat(new Elephant(500000)) compile error class Allesfresser(name: String) { def eat(food: { def cal : Int}) = { println("Ich habe " + food + " mit " + food.cal + " cal gegessen.") } } val allesFresser = new Allesfresser("Anna") allesFresser.eat(Food("Schnitzel", 400)) allesFresser.eat(new Elephant(500000))
Friday, September 28, 2012
Structural Food Typing
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment