课程: Treating Go as an Object-Oriented Language
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
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…
内容
-
-
-
-
(已锁定)
Structs as classes2 分钟 21 秒
-
(已锁定)
Creating a struct5 分钟 16 秒
-
(已锁定)
Encapsulation2 分钟 43 秒
-
(已锁定)
Leveraging encapsulation5 分钟 35 秒
-
(已锁定)
Solution: Struct as class2 分钟 2 秒
-
(已锁定)
Composition1 分钟 36 秒
-
(已锁定)
Leveraging composition4 分钟 11 秒
-
(已锁定)
Polymorphism2 分钟 59 秒
-
(已锁定)
Leveraging polymorphism4 分钟 49 秒
-
(已锁定)
Solution: OOP4 分钟 56 秒
-
(已锁定)
-