Created
August 8, 2021 10:30
-
-
Save BWbwchen/1fcaf0c54b836668e429343a11b61603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Human struct { | |
name string | |
} | |
type Student struct { | |
Human | |
job string | |
} | |
type Worker struct { | |
Human | |
job string | |
salary int | |
} | |
func (h Human) Info() { | |
fmt.Printf("I am %s\n", h.name) | |
} | |
func (s Student) Info () { | |
fmt.Printf("I am %s. I am a %s.\n", s.name, s.job) | |
} | |
func (w Worker) Info () { | |
fmt.Printf("I am %s. I am a %s. I have %d salary\n", w.name, w.job, w.salary) | |
} | |
type Person interface { | |
Info() | |
} | |
func main() { | |
Tom := Student{Human{"Tom"}, "student"} | |
Ben := Worker{Human{"Ben"}, "worker", 1000} | |
Susan := Human{"Susan"} | |
Tom.Info() | |
Ben.Info() | |
Susan.Info() | |
var i Person | |
// Becaus Student and Worker and Human all have implemented Info method | |
i = Tom | |
i.Info() | |
i = Ben | |
i.Info() | |
i = Susan | |
i.Info() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment