-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-struct.jl
72 lines (46 loc) · 905 Bytes
/
09-struct.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# struct
struct Person
name::String
age::Int64
end
jose = Person("Nose", 34)
typeof(jose)
jose.name
# fields
fieldnames(Person)
fieldtypes(Person)
# mutable structs
#jose.name = "José"
mutable struct MutablePerson
name::String
age::Int64
end
jose_mutable = MutablePerson("Jose", 34)
jose_mutable.name = "José"
jose_mutable.age = 35
jose_mutable
function newborn!(x::MutablePerson)
x.age = 0
end
newborn!(jose_mutable)
jose_mutable
# abstract types
abstract type Pet end
struct Dog <: Pet end
struct Cat <: Pet end
function encounter(x::Pet, y::Pet)
return "fallback"
end
function encounter(x::Dog, y::Cat)
return "Oh, there's a chase"
end
function encounter(x::Cat, y::Dog)
return "Oh, there's hissing"
end
rex = Dog()
meow = Cat()
encounter(rex, meow)
encounter(meow, rex)
struct Giraffe <: Pet end
godfried = Giraffe()
encounter(rex, godfried)