225. Scala?
abstract class Bird
class Penguin extends Bird with Swimming
class Pigeon extends Bird with Flying {
val flyMessage = I'm Pigeon. I can fly
}
class Hawk extends Bird with Swimming with Flying {
val flyMessage = I'm Hawk. I can fly
}
415. Scala
class User {
var name: String = _
var orders: List[Order] = Nil
}
class Order {
var id: Int = _
var products: List[Product] = Nil
}
class Product {
var id: Int = _
var category: String = _
}
423. Scala
case class User(name: String, orders: List[Order])
case class Order(id: Int, products: List[Product])
case class Product(id: Int, category: String)
501. 탐구 Pattern matching
public class CafeJava {
private static String order(String menu) {
switch(menu) {
case Latte :
return Mix Espresso + Milk;
case Mocha :
return Mix Chocolate + Espresso + Milk;
case Milk :
return Just Milk;
case Americano :
return Espresso + Water;
default :
return Sorry.;
}
}
public static void main(String[] args) {
System.out.println(Order Americano : + order(Americano));
System.out.println(Order Juice : + order(Juice));
}
}
Java
502. object CafeScala {
def order(menu: String): String = menu match {
case Latter = Mix Espresso + Milk
case Mocha = Mix Chocolate + Espresso + Milk
case Milk = Just Milk
case Americano = Espresso + Water
case _ = Sorry.
}
def main(args: Array[String]): Unit = {
println(Order Americano : + order(Americano))
println(Order Juice : + order(Juice))
}
}
Scala
Pattern matchingScala
552. 탐구 case class
abstract class Shape
case class Rectangle(width: Double, height: Double) extends
Shape
case class Circle(radius: Double) extends Shape
case class Square(side: Double) extends Shape
554. 탐구 case class
Rectangle area: 15.0
object PatternMatching {
def area(s: Shape): Double = s match {
case Rectangle(width, height) = width * height
case Circle(radius) = radius * radius * 3.14
case Square(side) = side * side
case _ = 0
}
def main(args: Array[String]): Unit = {
println(Rectangle area: + area(Rectangle(5.0, 3.0)))
}
Result