File tree 1 file changed +53
-2
lines changed 1 file changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -367,7 +367,53 @@ Hi from walk function
367
367
структуру, как показано ниже.
368
368
369
369
` ` ` 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) {}
371
417
` ` `
372
418
373
419
Посмотрите как в вышеприведенной программе мы смогли создать иерархию. Это
@@ -382,10 +428,15 @@ Hi from walk function
382
428
383
429
` ` ` shell
384
430
go run inheritance/example7/program7.go
431
+ iAnimal
432
+ --iAquatic
433
+ ----shark
434
+ --iNonAquatic
435
+ ----lion
385
436
` ` `
386
437
387
438
# # Заключение
388
439
389
440
Go не поддерживает наследование типов, но того же можно добиться с помощью
390
441
встраивания, но нужно быть внимательным при создании такой иерархии типов.
391
- Кроме того, Go не поддерживает переопределение метода.
442
+ Кроме того, Go не поддерживает переопределение метода.
You can’t perform that action at this time.
0 commit comments