首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 15:18:32 +08:00
commit d6393266b0
193 changed files with 14287 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
package main
import "fmt"
// 定义接口(可发声的物体)
type Sounder interface {
MakeSound() string
}
// 狗实现Sounder接口
type Dog struct{}
func (d Dog) MakeSound() string {
return "汪汪"
}
// 猫实现Sounder接口
type Cat struct{}
func (c Cat) MakeSound() string {
return "喵喵"
}
// 多态:接收Sounder接口,调用不同实现
func animalSound(s Sounder) {
fmt.Println(s.MakeSound())
}
func main() {
var s Sounder
s = Dog{}
animalSound(s) // 汪汪
s = Cat{}
animalSound(s) // 喵喵
}