A square can't have different width and height
This commit is contained in:
@@ -13,8 +13,8 @@ type geometry interface {
|
||||
}
|
||||
|
||||
// For our example we'll implement this interface on
|
||||
// `square` and `circle` types.
|
||||
type square struct {
|
||||
// `rect` and `circle` types.
|
||||
type rect struct {
|
||||
width, height float64
|
||||
}
|
||||
type circle struct {
|
||||
@@ -23,12 +23,12 @@ type circle struct {
|
||||
|
||||
// To implement an interface in Go, we just need to
|
||||
// implement all the methods in the interface. Here we
|
||||
// implement `geometry` on `square`s.
|
||||
func (s square) area() float64 {
|
||||
return s.width * s.height
|
||||
// implement `geometry` on `rect`s.
|
||||
func (r rect) area() float64 {
|
||||
return r.width * r.height
|
||||
}
|
||||
func (s square) perim() float64 {
|
||||
return 2*s.width + 2*s.height
|
||||
func (r rect) perim() float64 {
|
||||
return 2*r.width + 2*r.height
|
||||
}
|
||||
|
||||
// The implementation for `circle`s.
|
||||
@@ -50,13 +50,13 @@ func measure(g geometry) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := square{width: 3, height: 4}
|
||||
r := rect{width: 3, height: 4}
|
||||
c := circle{radius: 5}
|
||||
|
||||
// The `circle` and `square` struct types both
|
||||
// The `circle` and `rect` struct types both
|
||||
// implement the `geometry` interface so we can use
|
||||
// instances of
|
||||
// these structs as arguments to `measure`.
|
||||
measure(s)
|
||||
measure(r)
|
||||
measure(c)
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
9bafbade1f5effc38be233c3fd12cbdd7c6e226d
|
||||
guxTzcucR-
|
||||
3547b935d1e0322c0fb696726c27cae53a275e0a
|
||||
313UebA3rD
|
||||
|
||||
Reference in New Issue
Block a user