课程: Treating Go as an Object-Oriented Language
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Struct as class
(upbeat music) - [Instructor] Now it's time for my solution to this challenge exercise. What I've done is down here in the GetArea method, I implemented it by doing a r.length times r.width which is the formula to calculate the area of a rectangle. And then I returned that. Notice that I'm referencing the attributes of the struct by its reference in the function definition. In the same way on GetPerimeter, I did two times the length plus two times the width, again, referencing the values from the struct, the attributes of the struct themselves, and then doing the appropriate math to get the perimeter of a rectangle. So now if I run my code, you will see that I get the results back that I would expect, which is that, in this case, the area is 32 and the perimeter is 24. And of course we can change the values so that width is 5 and, let's say, 10 for the length. And test that code again. And now you'll see that we get…
内容
-
-
-
-
(已锁定)
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 秒
-
(已锁定)
-