183 lines
3.0 KiB
Go
183 lines
3.0 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import "fmt"
|
||
|
|
|
||
|
|
type Address struct {
|
||
|
|
City string
|
||
|
|
Street string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Person struct {
|
||
|
|
Name string
|
||
|
|
Age int
|
||
|
|
Address Address
|
||
|
|
}
|
||
|
|
|
||
|
|
type Employee struct {
|
||
|
|
Person
|
||
|
|
EmployeeID int
|
||
|
|
Department string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Employee2 struct {
|
||
|
|
____
|
||
|
|
Salary float64
|
||
|
|
}
|
||
|
|
|
||
|
|
type Reader interface {
|
||
|
|
Read() string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Writer interface {
|
||
|
|
Write(s string)
|
||
|
|
}
|
||
|
|
|
||
|
|
type File struct {
|
||
|
|
Name string
|
||
|
|
Content string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f File) Read() string {
|
||
|
|
return f.___
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *File) Write(s string) {
|
||
|
|
f.Content = ____
|
||
|
|
}
|
||
|
|
|
||
|
|
type ReadWriter interface {
|
||
|
|
____
|
||
|
|
____
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise1() string {
|
||
|
|
fmt.Println("=== 练习1:结构体嵌入 ===")
|
||
|
|
|
||
|
|
emp := Employee{
|
||
|
|
Person: Person{
|
||
|
|
Name: "张三",
|
||
|
|
Age: 30,
|
||
|
|
},
|
||
|
|
EmployeeID: 1001,
|
||
|
|
Department: "技术部",
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Printf("员工姓名: %s, 年龄: %d\n", ____, emp.___)
|
||
|
|
|
||
|
|
return ____
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise2() string {
|
||
|
|
fmt.Println("=== 练习2:多层嵌入 ===")
|
||
|
|
|
||
|
|
address := Address{City: "北京", Street: "长安街"}
|
||
|
|
|
||
|
|
emp2 := Employee2{
|
||
|
|
Person: Person{
|
||
|
|
Name: "李四",
|
||
|
|
Address: ____,
|
||
|
|
},
|
||
|
|
Salary: 10000.0,
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Printf("员工: %s, 城市: %s\n", emp2.___, emp2.___.City)
|
||
|
|
|
||
|
|
return ____
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise3() string {
|
||
|
|
fmt.Println("=== 练习3:接口实现 ===")
|
||
|
|
|
||
|
|
file := File{Name: "test.txt", Content: "初始内容"}
|
||
|
|
|
||
|
|
// 使用Read方法
|
||
|
|
content := file.____()
|
||
|
|
fmt.Printf("读取内容: %s\n", content)
|
||
|
|
|
||
|
|
// 使用Write方法
|
||
|
|
file.____("新内容")
|
||
|
|
fmt.Printf("写入后内容: %s\n", file.___)
|
||
|
|
|
||
|
|
return ___
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise4() string {
|
||
|
|
fmt.Println("=== 练习4:接口组合 ===")
|
||
|
|
|
||
|
|
file := &File{Name: "data.txt", "原始数据"}
|
||
|
|
|
||
|
|
var rw _____
|
||
|
|
|
||
|
|
rw.Write("修改后的数据")
|
||
|
|
result := rw.Read()
|
||
|
|
|
||
|
|
fmt.Printf("读写结果: %s\n", result)
|
||
|
|
|
||
|
|
return ____
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise5() string {
|
||
|
|
fmt.Println("=== 练习5:类型断言 ===")
|
||
|
|
|
||
|
|
var data interface{} = "Hello, Go!"
|
||
|
|
|
||
|
|
// 尝试断言为string
|
||
|
|
str, ok := data.____
|
||
|
|
if ok {
|
||
|
|
fmt.Printf("类型断言成功: %s\n", ____)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 尝试断言为int
|
||
|
|
_, ok = data.____
|
||
|
|
if !ok {
|
||
|
|
fmt.Println("类型断言失败: 不是int类型")
|
||
|
|
}
|
||
|
|
|
||
|
|
return "类型断言演示完成"
|
||
|
|
}
|
||
|
|
|
||
|
|
func exercise6() string {
|
||
|
|
fmt.Println("=== 练习6:类型选择 ===")
|
||
|
|
|
||
|
|
values := []interface{}{10, "hello", 3.14, true}
|
||
|
|
|
||
|
|
for _, v := range ____ {
|
||
|
|
switch v.(type) {
|
||
|
|
case int:
|
||
|
|
fmt.Printf("整数: %d\n", ____)
|
||
|
|
case string:
|
||
|
|
fmt.Printf("字符串: %s\n", ____)
|
||
|
|
case float64:
|
||
|
|
fmt.Printf("浮点数: %.2f\n", ____)
|
||
|
|
case bool:
|
||
|
|
fmt.Printf("布尔值: %t\n", ____)
|
||
|
|
default:
|
||
|
|
fmt.Println("未知类型")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return "类型选择演示完成"
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
fmt.Println("=== 组合与类型断言 练习 ===\n")
|
||
|
|
|
||
|
|
result1 := exercise1()
|
||
|
|
fmt.Printf("练习1结果: %s\n", result1)
|
||
|
|
|
||
|
|
result2 := exercise2()
|
||
|
|
fmt.Printf("练习2结果: %s\n", result2)
|
||
|
|
|
||
|
|
result3 := exercise3()
|
||
|
|
fmt.Printf("练习3结果: %s\n", result3)
|
||
|
|
|
||
|
|
result4 := exercise4()
|
||
|
|
fmt.Printf("练习4结果: %s\n", result4)
|
||
|
|
|
||
|
|
result5 := exercise5()
|
||
|
|
fmt.Printf("练习5结果: %s\n", result5)
|
||
|
|
|
||
|
|
result6 := exercise6()
|
||
|
|
fmt.Printf("练习6结果: %s\n", result6)
|
||
|
|
}
|