forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
631 B
47 lines
631 B
3 years ago
|
|
||
|
```go
|
||
|
package template
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type WorkInterface interface {
|
||
|
GetUp()
|
||
|
Work()
|
||
|
Sleep()
|
||
|
}
|
||
|
|
||
|
type Worker struct {
|
||
|
WorkInterface
|
||
|
}
|
||
|
|
||
|
func NewWorker(w WorkInterface) *Worker {
|
||
|
return &Worker{w}
|
||
|
}
|
||
|
|
||
|
func (w *Worker) Daily() {
|
||
|
w.GetUp()
|
||
|
w.Work()
|
||
|
w.Sleep()
|
||
|
}
|
||
|
|
||
|
type Coder struct {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (c *Coder) GetUp() {
|
||
|
fmt.Println("::操作:GetUp")
|
||
|
}
|
||
|
|
||
|
func (c *Coder) Work() {
|
||
|
fmt.Println("::操作:Work")
|
||
|
}
|
||
|
|
||
|
func (c *Coder) Sleep() {
|
||
|
fmt.Println("::操作:Sleep")
|
||
|
}
|
||
|
```
|
||
|
* 模版模式
|
||
|
|
||
|
* Worker 继承接口,Coder实现接口,及Coder是Worker的子类
|
||
|
|
||
|
* 当将Coder作为参数传递给Worker时,实现多态调用
|