-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTraitDemo2.scala
88 lines (68 loc) · 2.32 KB
/
TraitDemo2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// https://docs.scala-lang.org/tour/tour-of-scala.html
// https://docs.scala-lang.org/overviews/scala-book/traits-intro.html
/*
Traits are used to share interfaces and fields between classes.
They are similar to Java 8’s interfaces. Classes and objects can extend traits,
but traits cannot be instantiated and therefore have no parameters.
*/
package ScalaBasic
object TraitDemo2 extends App{
// PART 1 : define trait
trait HairColor // trait has no parameters
// Traits become especially useful as generic types and with abstract methods
// Extending the trait Iterator[A] requires a type A and implementations of the methods hasNext and next
trait Iterator[A]{
def hasNext: Boolean
def next(): A
}
// using trait
// Use the extends keyword to extend a trait. Then implement any abstract members of the trait using the override keyword
class IntIterator(to: Int) extends Iterator[Int]{
private var current = 0
override def hasNext: Boolean = current < to
override def next(): Int = {
if (hasNext){
val t = current
current += 1
t
} else 0
}
}
// run
val iterator = new IntIterator(10)
println(iterator.next()) // returns 0
println(iterator.next()) // returns 1
println(iterator.next()) // returns 2
// PART 2 Subtyping
import scala.collection.mutable.ArrayBuffer
trait Pet {
val name: String
}
class Cat(val name: String) extends Pet
class Dog(val name: String) extends Pet
val dog = new Dog("poppy")
val cat = new Cat("tom")
val animals = ArrayBuffer.empty[Pet]
animals.append(dog)
animals.append(cat)
animals.foreach(pet => println(pet.name))
// PART 3 MY PRACTICE
trait CarSize
trait CarColor
trait CarClass{
def getAge: Int
def getName: String
def getPrice: Int
}
class GetCarProfile(age: Int, name: String, price: Int) extends CarClass{
override def getAge: Int = age
override def getName: String = name
override def getPrice: Int = price
def ifexpensive: Boolean = price > 99999
}
val benz = new GetCarProfile(3, "BENZ", 1000)
println(benz.getAge)
println(benz.getName)
println(benz.getPrice)
println(benz.ifexpensive)
}