-
Notifications
You must be signed in to change notification settings - Fork 1
/
PersonModule.jl
53 lines (42 loc) · 1.07 KB
/
PersonModule.jl
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
module PersonModule
export PersonBase, Person, Student, print, testCases
abstract type PersonBase end
mutable struct Person <: PersonBase
lastname :: String
firstname :: String
Person(name,firstname) = new(name,firstname)
end
mutable struct Student <: PersonBase
lastname :: String
firstname :: String
number :: Integer
Student(name,firstname,number) = new(name,firstname,number)
end
function print(p::Person)
println(string(p.firstname," ",p.lastname))
end
function print(s::Student)
println(string(s.firstname," ",s.lastname," ",s.number))
end
function print(b::PersonBase)
print(b)
end
function testCases()
# TEST 1 : An instance
p = Person("Duchemin","paul")
print(p)
# TEST 2 : A Collection
al1 = [Person("Duchemin$i","paul") for i in 1:10]
for p in al1
print(p)
end
# TEST 3 : Polymorphism
al2 = []
for i in 1:10
i%2 == 0 ? push!(al2,Person("Duchemin$i","paul")) : push!(al2,Student("Durand$i","jules",i*1000))
end
for p in al2
print(p)
end
end
end # module