Skip to content

Commit

Permalink
Update inheritance.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimDzhangirov authored Apr 15, 2022
1 parent adc0374 commit 031ee3d
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions docs/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,53 @@ Hi from walk function
структуру, как показано ниже.
```go
package main
import "fmt"
type iAnimal interface {
breathe()
}
type animal struct {
}
func (a *animal) breathe() {
fmt.Println("Animal breate")
}
type iAquatic interface {
iAnimal
swim()
}
type aquatic struct {
animal
}
func (a *aquatic) swim() {
fmt.Println("Aquatic swim")
}
type iNonAquatic interface {
iAnimal
walk()
}
type nonAquatic struct {
animal
}
func (a *nonAquatic) walk() {
fmt.Println("Non-Aquatic walk")
}
type shark struct {
aquatic
}
type lion struct {
nonAquatic
}
func main() {
shark := &shark{}
checkAquatic(shark)
checkAnimal(shark)
lion := &lion{}
checkNonAquatic(lion)
checkAnimal(lion)
}
func checkAquatic(a iAquatic) {}
func checkNonAquatic(a iNonAquatic) {}
func checkAnimal(a iAnimal) {}
```
Посмотрите как в вышеприведенной программе мы смогли создать иерархию. Это
Expand All @@ -382,10 +428,15 @@ Hi from walk function
```shell
go run inheritance/example7/program7.go
iAnimal
--iAquatic
----shark
--iNonAquatic
----lion
```
## Заключение
Go не поддерживает наследование типов, но того же можно добиться с помощью
встраивания, но нужно быть внимательным при создании такой иерархии типов.
Кроме того, Go не поддерживает переопределение метода.
Кроме того, Go не поддерживает переопределение метода.

0 comments on commit 031ee3d

Please sign in to comment.