GO 没有继承,但是有组合。
Composition by embedding structs
package main
import (
"fmt"
)
type author struct {
firstName string
lastName string
bio string
}
func (a author) fullName() string {
return fmt.Sprintf("%s %s", a.firstName, a.lastName)
}
type blogPost struct {
title string
content string
author // [anonymous] field `author`.
}
func (b blogPost) details() {
fmt.Println("Title: ", b.title)
fmt.Println("Content: ", b.content)
fmt.Println("Author: ", b.fullName())
fmt.Println("Bio: ", b.bio)
}
func main() {
author1 := author{
"Naveen",
"Ramanathan",
"Golang Enthusiast",
}
blogPost1 := blogPost{
"Inheritance in Go",
"Go supports composition instead of inheritance",
author1,
}
blogPost1.details()
}
Embedding slice of structs
it is not possible to anonymously embed a slice.
// error
type website struct {
[]blogPost
}
// right
type website struct {
blogPosts []blogPost
}
func (w website) contents() {
fmt.Println("Contents of Website\n")
for _, v := range w.blogPosts {
v.details()
fmt.Println()
}
}
package main
import (
"fmt"
)
type author struct {
firstName string
lastName string
bio string
}
func (a author) fullName() string {
return fmt.Sprintf("%s %s", a.firstName, a.lastName)
}
type blogPost struct {
title string
content string
author
}
func (p blogPost) details() {
fmt.Println("Title: ", p.title)
fmt.Println("Content: ", p.content)
fmt.Println("Author: ", p.fullName())
fmt.Println("Bio: ", p.bio)
}
type website struct {
blogPosts []blogPost
}
func (w website) contents() {
fmt.Println("Contents of Website\n")
for _, v := range w.blogPosts {
v.details()
fmt.Println()
}
}
func main() {
author1 := author{
"Naveen",
"Ramanathan",
"Golang Enthusiast",
}
blogPost1 := blogPost{
"Inheritance in Go",
"Go supports composition instead of inheritance",
author1,
}
blogPost2 := blogPost{
"Struct instead of Classes in Go",
"Go does not support classes but methods can be added to structs",
author1,
}
blogPost3 := blogPost{
"Concurrency",
"Go is a concurrent language and not a parallel one",
author1,
}
w := website{
blogPosts: []blogPost{blogPost1, blogPost2, blogPost3},
}
w.contents()
}