Skip to content

Commit 031ee3d

Browse files
Update inheritance.md
1 parent adc0374 commit 031ee3d

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

docs/inheritance.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,53 @@ Hi from walk function
367367
структуру, как показано ниже.
368368
369369
```go
370-
370+
package main
371+
import "fmt"
372+
type iAnimal interface {
373+
breathe()
374+
}
375+
type animal struct {
376+
}
377+
func (a *animal) breathe() {
378+
fmt.Println("Animal breate")
379+
}
380+
type iAquatic interface {
381+
iAnimal
382+
swim()
383+
}
384+
type aquatic struct {
385+
animal
386+
}
387+
func (a *aquatic) swim() {
388+
fmt.Println("Aquatic swim")
389+
}
390+
type iNonAquatic interface {
391+
iAnimal
392+
walk()
393+
}
394+
type nonAquatic struct {
395+
animal
396+
}
397+
func (a *nonAquatic) walk() {
398+
fmt.Println("Non-Aquatic walk")
399+
}
400+
type shark struct {
401+
aquatic
402+
}
403+
type lion struct {
404+
nonAquatic
405+
}
406+
func main() {
407+
shark := &shark{}
408+
checkAquatic(shark)
409+
checkAnimal(shark)
410+
lion := &lion{}
411+
checkNonAquatic(lion)
412+
checkAnimal(lion)
413+
}
414+
func checkAquatic(a iAquatic) {}
415+
func checkNonAquatic(a iNonAquatic) {}
416+
func checkAnimal(a iAnimal) {}
371417
```
372418
373419
Посмотрите как в вышеприведенной программе мы смогли создать иерархию. Это
@@ -382,10 +428,15 @@ Hi from walk function
382428
383429
```shell
384430
go run inheritance/example7/program7.go
431+
iAnimal
432+
--iAquatic
433+
----shark
434+
--iNonAquatic
435+
----lion
385436
```
386437
387438
## Заключение
388439
389440
Go не поддерживает наследование типов, но того же можно добиться с помощью
390441
встраивания, но нужно быть внимательным при создании такой иерархии типов.
391-
Кроме того, Go не поддерживает переопределение метода.
442+
Кроме того, Go не поддерживает переопределение метода.

0 commit comments

Comments
 (0)