Skip to content

Commit f9fcf1e

Browse files
authored
Update 4、interface.md
1 parent 15ed23f commit f9fcf1e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

4、interface.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ type People interface {
1919
Speak(string) string
2020
}
2121

22-
type Stduent struct{}
22+
type Student struct{}
2323

24-
func (stu *Stduent) Speak(think string) (talk string) {
24+
func (stu *Student) Speak(think string) (talk string) {
2525
if think == "love" {
2626
talk = "You are a good boy"
2727
} else {
@@ -31,7 +31,7 @@ func (stu *Stduent) Speak(think string) (talk string) {
3131
}
3232

3333
func main() {
34-
var peo People = Stduent{}
34+
var peo People = Student{}
3535
think := "love"
3636
fmt.Println(peo.Speak(think))
3737
}
@@ -51,7 +51,7 @@ func main() {
5151

5252
那么,满足上述3个条件,就可以产生多态效果,就是,父类指针可以调用子类的具体方法。
5353

54-
所以上述代码报错的地方在`var peo People = Stduent{}`这条语句, `Student{}`已经重写了父类`People{}`中的`Speak(string) string`方法,那么只需要用父类指针指向子类对象即可。
54+
所以上述代码报错的地方在`var peo People = Student{}`这条语句, `Student{}`已经重写了父类`People{}`中的`Speak(string) string`方法,那么只需要用父类指针指向子类对象即可。
5555

5656
所以应该改成`var peo People = &Student{}` 即可编译通过。(People为interface类型,就是指针类型)
5757

0 commit comments

Comments
 (0)