From the course: Treating Go as an Object-Oriented Language
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Leveraging encapsulation - Go Tutorial
From the course: Treating Go as an Object-Oriented Language
Leveraging encapsulation
- [Narrator] All right, in our pets package, let's open up dog.go. And we are going to put an attribute called last slept, notice the lowercase L, of type time. Now let's create a couple more functions. So we were going to do a function on our dog called needs sleep. Again, internal. This will return a bool. And we are going to return time.now.sub d.lastslept And if it is greater than four times time.hour. So if it has been four hours since this dog slept we'll return true, otherwise we'll return false. And now we will add a function called sleep, and this will not return anything. And we will simply do d.lastslept equals time.now. Now let's use this. So in our give attention function we will do if d.needssleep, d.sleep. And then we'll return, your dog is tired and needs to rest. Now let's jump over into main and there's a few things that I want to show you. So the first thing that I want to show you is that the…
Contents
-
-
-
-
(Locked)
Structs as classes2m 21s
-
(Locked)
Creating a struct5m 16s
-
(Locked)
Encapsulation2m 43s
-
(Locked)
Leveraging encapsulation5m 35s
-
(Locked)
Solution: Struct as class2m 2s
-
(Locked)
Composition1m 36s
-
(Locked)
Leveraging composition4m 11s
-
(Locked)
Polymorphism2m 59s
-
(Locked)
Leveraging polymorphism4m 49s
-
(Locked)
Solution: OOP4m 56s
-
(Locked)
-