课程: Treating Go as an Object-Oriented Language

今天就学习课程吧!

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

Solution: OOP

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…

内容