-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbstractMembers.scala
56 lines (51 loc) · 1.28 KB
/
AbstractMembers.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
trait Abstract {
type T
def transform(x: T): T
val initial: T
var current: T
}
class Concrete extends Abstract {
type T = String
def transform(x: String) = x + x
val initial = "hi"
var current = initial
}
// abstract vars
trait AbstractTime {
def hour: Int // getter for ‘hour’
def hour_=(x: Int) // setter for ‘hour’
def minute: Int // getter for ‘minute’
def minute_=(x: Int) // setter for ‘minute’
}
trait RationalTrait {
val numerArg: Int
val denomArg: Int
require(denomArg != 0)
private val g = gcd(numerArg, denomArg)
val numer = numerArg / g
val denom = denomArg / g
private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
override def toString = numer +"/"+ denom
}
//Pre-initialized fields
object twoThirds extends {
val numerArg = 2
val denomArg = 3
} with RationalTrait
// Abstract types
class Food
abstract class Animal {
type SuitableFood <: Food // upper bound
def eat(food: SuitableFood)
}
class Grass extends Food
class Grass2 extends Grass
class Cow extends Animal {
type SuitableFood = Grass
override def eat(food: Grass) {println("I can eat them")}
}
val grass = new Grass()
val grass2 = new Grass2()
val cow = new Cow()
cow eat grass
cow eat grass2