Skip to content

Commit 8de69e2

Browse files
committed
Add name field and modify output for better understanding
1 parent 0244f6a commit 8de69e2

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

content/chapter 4/oop/4.23.5-abstraction.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ type Shape interface {
3030

3131
// Rectangle is a struct that implements the Shape interface.
3232
type Rectangle struct {
33-
width, height float64
33+
name string
34+
width float64
35+
height float64
3436
}
3537

3638
// Area returns the area of the rectangle.
@@ -45,6 +47,7 @@ func (r Rectangle) Perimeter() float64 {
4547

4648
// Circle is a struct that implements the Shape interface.
4749
type Circle struct {
50+
name string
4851
radius float64
4952
}
5053

@@ -60,16 +63,16 @@ func (c Circle) Perimeter() float64 {
6063

6164
func main() {
6265
// Create a rectangle and a circle.
63-
r := Rectangle{width: 10, height: 20}
64-
c := Circle{radius: 5}
66+
r := Rectangle{name: "Rectangle", width: 10, height: 20}
67+
c := Circle{name: "Circle", radius: 5}
6568

6669
// Declare a slice of Shape interfaces.
6770
shapes := []Shape{r, c}
6871

6972
// Iterate over the shapes and print their area and perimeter.
7073
for _, shape := range shapes {
71-
fmt.Println("Area:", shape.Area())
72-
fmt.Println("Perimeter:", shape.Perimeter())
74+
fmt.Printf("Area of shape %v is: %.2f\n", shape, shape.Area())
75+
fmt.Printf("Perimeter of shape %v is: %.2f\n", shape, shape.Perimeter())
7376
fmt.Println("")
7477
}
7578
}

0 commit comments

Comments
 (0)