课程: Treating Go as an Object-Oriented Language
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Leveraging composition
- [Instructor] So, now I want to show you how to leverage composition. In your pets package, let's create a new file called animal. We will create a type animal that is a struct, and we will put a hidden field, so an encapsulated field called lastAte with a time. So, now, let's put a function on our animal. And this is going to look very familiar. We're going to call it feed, and we will pass it food as a string, return a string. We will set a dot last ate equal to time dot now. So, when we're feeding him, we're going to say that he just ate and we will return format dot sprint f. The animal is eating percent s, and we will pass it the food. Now, let's add another method called, is hungry, and this will return a bool and we will simply return time dot now, let me make this an actual bool here, time dot now dot sub, and let's see. My dog eats all the time, no matter how often he ate. So, we will do, what, two seconds, how…
内容
-
-
-
-
(已锁定)
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 秒
-
(已锁定)
-