-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpperCass.scala
40 lines (29 loc) · 893 Bytes
/
UpperCass.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ScalaBasic
// https://openhome.cc/Gossip/Scala/UpperboundLowerboundViewbound.html
// https://ithelp.ithome.com.tw/articles/10194355
// https://docs.scala-lang.org/tour/lower-type-bounds.html
object UpperCass extends App{
abstract class Animal {
def name: String
}
abstract class Pet extends Animal {}
class Cat extends Pet {
override def name: String = "Cat"
}
class Dog extends Pet {
override def name: String = "Dog"
}
class Lion extends Animal {
override def name: String = "Lion"
}
class PetContainer[P <: Pet](p: P) {
def pet: P = p
}
// run
val dogContainer = new PetContainer[Dog](new Dog)
val catContainer = new PetContainer[Cat](new Cat)
println(dogContainer)
println(catContainer)
// this would not compile
//val lionContainer = new PetContainer[Lion](new Lion)
}