mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
40 lines
609 B
Go
Executable File
40 lines
609 B
Go
Executable File
package main
|
|
|
|
import "fmt"
|
|
|
|
type Base struct {
|
|
id string
|
|
}
|
|
|
|
func (b *Base) Id() string {
|
|
return b.id
|
|
}
|
|
|
|
func (b *Base) SetId(id string) {
|
|
b.id = id
|
|
}
|
|
|
|
type Person struct {
|
|
Base
|
|
FirstName string
|
|
LastName string
|
|
}
|
|
|
|
type Employee struct {
|
|
Person
|
|
salary float32
|
|
}
|
|
|
|
func main() {
|
|
idjb := Base{"007"}
|
|
jb := Person{idjb, "James", "Bond"}
|
|
e := &Employee{jb, 100000.}
|
|
fmt.Printf("ID of our hero: %v\n", e.Id())
|
|
// Change the id:
|
|
e.SetId("007B")
|
|
fmt.Printf("The new ID of our hero: %v\n", e.Id())
|
|
}
|
|
/* Output:
|
|
ID of our hero: 007
|
|
The new ID of our hero: 007B
|
|
*/ |