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