课程: Treating Go as an Object-Oriented Language

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Leveraging encapsulation

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…

内容