package main
import (
"fmt"
"math"
)
// Shape 基础结构体,包含计算面积的方法
type Shape interface {
Area() float64
}
// Rectangle 结构体,通过组合实现 Shape 接口
type Rectangle struct {
Width float64
Height float64
}
// Area 计算矩形面积
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
// Circle 结构体,通过组合实现 Shape 接口
type Circle struct {
Radius float64
}
// Area 计算圆形面积
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
func main() {
// 创建 Rectangle 实例
rectangle := Rectangle{Width: 5, Height: 3}
// 创建 Circle 实例
circle := Circle{Radius: 4}
// 调用 Area 方法并打印结果
fmt.Printf("Rectangle Area: %.2f\n", rectangle.Area())
fmt.Printf("Circle Area: %.2f\n", circle.Area())
}