Skip to content

Instantly share code, notes, and snippets.

@scbizu
Created May 30, 2016 16:50
Show Gist options
  • Save scbizu/b85f02b13aa0d043d26fecf96c1f3310 to your computer and use it in GitHub Desktop.
Save scbizu/b85f02b13aa0d043d26fecf96c1f3310 to your computer and use it in GitHub Desktop.
golang oop practice(method and some oop features)
package main
import "fmt"
type human struct {
age int
name string
}
//继承
type student struct {
human
stuNo int
}
func (h human) getAge() int {
return h.age
}
func (h human) getName() string {
return h.name
}
func (h *human) setAge(age int) {
h.age = age
}
func (h human) action() {
fmt.Println("I'am", h.name, "I am ", h.age, "years old")
}
//重写
func (s student) action() {
fmt.Println("I am ", s.age, "years old", "I'am", s.name, ".")
}
func main() {
h1 := human{1, "jjj"}
h1.setAge(3)
h1.action()
s1 := student{human{2, "aaa"}, 2013}
s1.setAge(4)
s1.action()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment