File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed
Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff 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
3333func 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
You can’t perform that action at this time.
0 commit comments