forked from ArchitectingSoftware/CNSE-Class-Demo-Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.go
More file actions
109 lines (92 loc) · 2.47 KB
/
Copy pathoop.go
File metadata and controls
109 lines (92 loc) · 2.47 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import "fmt"
// define some base types
type individual struct {
name string
age uint
}
type student struct {
individual
gpa float64
}
// Now define some additional types that use embedding, this is a form
// of inheritance that is based on composition rather than inheritance
// as in traditional OOP languages. This is a more flexible approach
// that allows for multiple inheritance and avoids the problems of
// the diamond problem.
type employee struct {
title string
salary float64
privacyRequest bool
}
type professor struct {
individual
employee
department string
nameSuffix string
}
// Now lets add some behavior to our types
func (i *individual) getName() string {
return i.name
}
func (i *individual) setName(name string) {
i.name = name
}
func (p *professor) setNameSuffix(suffix string) {
p.setName(p.getName() + " " + suffix)
}
func (i *individual) getAge() uint {
return i.age
}
func (p *professor) getAge() uint {
//This is kind of a made up example, but it shows how you can
//override methods in embedded types. In this case we are
//checking to see if the privacyRequest flag is set, if so
//we return 0, otherwise we return the age from the embedded
//individual type.
//
//This is similar to using "super" in Java or C++.
age := p.individual.getAge()
if p.privacyRequest {
return 0
} else {
return age
}
}
func oopDemo1() {
//Lets create a new professor, note we represent the
//composed types as fields in the struct using the typename
prof := professor{
individual: individual{name: "John Doe",
age: 42,
},
employee: employee{title: "Professor",
salary: 100000,
privacyRequest: true,
},
department: "Computer Science",
nameSuffix: "PhD",
}
// Now lets call some methods on our professor. Note this will
//automatically call getName from individual
fmt.Println("Prof getName()", prof.getName())
// Now lets call getAge, note that this will call the overriden getAge
// method in professor
prof.setNameSuffix("PhD")
println("Prof: getName() - post Suffix Set", prof.getName())
// Now lets create a student
s := student{
individual: individual{
name: "Jane Student",
age: 20},
gpa: 3.5,
}
//note that this will call the individual getName method because
//student does not override it
fmt.Println("Student getName()", s.getName())
}
func RunOopDemo() {
fmt.Println("------ Running OOP Demo ------")
oopDemo1()
fmt.Printf("-----------------------------------\n\n")
}