课程: Treating Go as an Object-Oriented Language
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: OOP
(upbeat music) - Now it's time for my solution to this challenge exercise. The first thing that I did here on line six was import math. And we're going to use that to calculate both the perimeter and the area of our circle. Now, in order to do that on the circle struct we need to have a radius of float 32. And on the rectangle struct we need length and width, both of which are also float 32. So now down here on line 28 we implement the new circle method. And this method returns a shape. So when you first typed this most likely you saw a compile error, because if you return a circle where you set the radius equal to the method perimeter radius, it would've complained that circle did not match the interface for shape because we had not yet implemented get area or get perimeter. So to solve that, I went ahead and implemented those. Now, I did a simple cheat here a little bit on get area. I used the built-in math.pi, which…
内容
-
-
-
-
(已锁定)
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 秒
-
(已锁定)
-