интерфейсы
This commit is contained in:
parent
d9f948bcfc
commit
ac4f5526ca
@ -1,5 +1,4 @@
|
|||||||
// _Interfaces_ are named collections of method
|
// _Интерфейсы_ это коллекции методов.
|
||||||
// signatures.
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -8,14 +7,14 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Here's a basic interface for geometric shapes.
|
// Пример базового интерфейса в Go
|
||||||
type geometry interface {
|
type geometry interface {
|
||||||
area() float64
|
area() float64
|
||||||
perim() float64
|
perim() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// For our example we'll implement this interface on
|
// В нашем примере мы будем реализовывать этот интерфейс
|
||||||
// `rect` and `circle` types.
|
// для типов `rect` и `circle`.
|
||||||
type rect struct {
|
type rect struct {
|
||||||
width, height float64
|
width, height float64
|
||||||
}
|
}
|
||||||
@ -23,9 +22,9 @@ type circle struct {
|
|||||||
radius float64
|
radius float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// To implement an interface in Go, we just need to
|
// Чтобы реализовать интерфейс на Go, нам просто нужно
|
||||||
// implement all the methods in the interface. Here we
|
// реализовать все методы в интерфейсе. Здесь мы
|
||||||
// implement `geometry` on `rect`s.
|
// реализуем интерфейс `geometry` для `rect`.
|
||||||
func (r rect) area() float64 {
|
func (r rect) area() float64 {
|
||||||
return r.width * r.height
|
return r.width * r.height
|
||||||
}
|
}
|
||||||
@ -33,7 +32,7 @@ func (r rect) perim() float64 {
|
|||||||
return 2*r.width + 2*r.height
|
return 2*r.width + 2*r.height
|
||||||
}
|
}
|
||||||
|
|
||||||
// The implementation for `circle`s.
|
// Реализация для `circle`.
|
||||||
func (c circle) area() float64 {
|
func (c circle) area() float64 {
|
||||||
return math.Pi * c.radius * c.radius
|
return math.Pi * c.radius * c.radius
|
||||||
}
|
}
|
||||||
@ -41,10 +40,10 @@ func (c circle) perim() float64 {
|
|||||||
return 2 * math.Pi * c.radius
|
return 2 * math.Pi * c.radius
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a variable has an interface type, then we can call
|
// Если переменная реализует интерфейс, то мы можем
|
||||||
// methods that are in the named interface. Here's a
|
// вызывать методы, которые находятся в этом интерфейсе.
|
||||||
// generic `measure` function taking advantage of this
|
// Функция `measure` использует это преимущество, для
|
||||||
// to work on any `geometry`.
|
// работы с любой фигурой.
|
||||||
func measure(g geometry) {
|
func measure(g geometry) {
|
||||||
fmt.Println(g)
|
fmt.Println(g)
|
||||||
fmt.Println(g.area())
|
fmt.Println(g.area())
|
||||||
@ -55,10 +54,10 @@ func main() {
|
|||||||
r := rect{width: 3, height: 4}
|
r := rect{width: 3, height: 4}
|
||||||
c := circle{radius: 5}
|
c := circle{radius: 5}
|
||||||
|
|
||||||
// The `circle` and `rect` struct types both
|
// Типы `circle` и `rect` структур реализуют
|
||||||
// implement the `geometry` interface so we can use
|
// интерфейс `geometry`, поэтому мы можем использовать
|
||||||
// instances of
|
// экземпляры этих структур в качестве аргументов для
|
||||||
// these structs as arguments to `measure`.
|
// `measure`.
|
||||||
measure(r)
|
measure(r)
|
||||||
measure(c)
|
measure(c)
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ $ go run interfaces.go
|
|||||||
78.53981633974483
|
78.53981633974483
|
||||||
31.41592653589793
|
31.41592653589793
|
||||||
|
|
||||||
# To learn more about Go's interfaces, check out this
|
# Чтобы узнать больше об интерфейсах Go, ознакомьтесь с
|
||||||
# [great blog post](http://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go).
|
# [этой статьей](http://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user