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 {
19
19
Speak (string ) string
20
20
}
21
21
22
- type Stduent struct {}
22
+ type Student struct {}
23
23
24
- func (stu *Stduent ) Speak (think string ) (talk string ) {
24
+ func (stu *Student ) Speak (think string ) (talk string ) {
25
25
if think == " love" {
26
26
talk = " You are a good boy"
27
27
} else {
@@ -31,7 +31,7 @@ func (stu *Stduent) Speak(think string) (talk string) {
31
31
}
32
32
33
33
func main () {
34
- var peo People = Stduent {}
34
+ var peo People = Student {}
35
35
think := " love"
36
36
fmt.Println (peo.Speak (think))
37
37
}
@@ -51,7 +51,7 @@ func main() {
51
51
52
52
那么,满足上述3个条件,就可以产生多态效果,就是,父类指针可以调用子类的具体方法。
53
53
54
- 所以上述代码报错的地方在` var peo People = Stduent {} ` 这条语句, ` Student{} ` 已经重写了父类` People{} ` 中的` Speak(string) string ` 方法,那么只需要用父类指针指向子类对象即可。
54
+ 所以上述代码报错的地方在` var peo People = Student {} ` 这条语句, ` Student{} ` 已经重写了父类` People{} ` 中的` Speak(string) string ` 方法,那么只需要用父类指针指向子类对象即可。
55
55
56
56
所以应该改成` var peo People = &Student{} ` 即可编译通过。(People为interface类型,就是指针类型)
57
57
You can’t perform that action at this time.
0 commit comments