Home > other >  I ask how to realize the override golang
I ask how to realize the override golang

Time:03-19

 
Package the main

The import "FMT"

Type IInfo interface {
GetName () the string
GetInfo () the string
}

Type a struct {
The Name string
}

Func (a) this * GetName () string {
Return "is a" + enclosing Name
}

Func (a) this * GetInfo () string {
Return this. GetName ()
}

Type b struct {
A
}

//rewrite the method
Func (this * b) GetName () string {
The return is "b" + this. The Name
}

Func main () {
A:=a {Name: "dog"}
FMT. Printf (" al-qeada etName: % s \ n ", al-qeada etName ())
FMT. Printf (" al-qeada etInfo: % s \ n ", al-qeada etInfo ())
B: b={a}
//output b.G etName: b is dog, that is to call the rewritten method
FMT. Printf (" b.G etName: % s \ n ", b.G etName ())
//output b.G etInfo: is a dog, that is called a method, hoping to get is the result of the call after rewriting method
FMT. Printf (" b.G etInfo: % s \ n ", b.G etInfo ())
}

CodePudding user response:

What do you mean? You GetInfo method only in a defined, b is not defined, of course, is to call a GetInfo method in, you in b rewrite the not to go,

CodePudding user response:

Simply
Is a base class, the realization of the two methods
Now based on a (inheritance) to realize a class b, class b is only one way to a different, so only need to rewrite the different method (overide)
If all the methods in the b rewrite it again, that is not inherited, it is completely rewrote the
Object programming is such a play
  • Related