Структуры
GitBook allows you to organize your book into chapters, each chapter is stored in a separate file like this one.
// Полиморыизм
package main
import "fmt"
import "math"
// https://play.golang.org/p/313UebA3rD
// Описание онсовного интерфейса
type ry interface {
ar() string
pe() string
le() string
}
// Описание типа 1
type rct struct {w,h,s string}
// Три аргумента !!! обязательно
// The implementation for `circle`s.
func (c rct) ar() string {return "Ответ ar : " + c.w + c.h + c.s}
func (c rct) pe() string {return "Ответ pe : " + c.w + c.h }
func (c rct) le() string {return "Ответ Le : " + c.h }
// Описание типа 2
type lct struct { w,h string}
// Три аргумента !!! обязательно для второй функции
func (c lct) ar() string {return "Ответ pe : " + c.w + c.h}
func (c lct) pe() string {return "Ответ pe : " + c.w + c.h}
func (c lct) le() string {return "Ответ Leee : " + c.h}
// Описание типа 3
type rcs struct {w,h,s,l string}
// Три аргумента !!! обязательно
// The implementation for `circle`s.
func (c rcs) ar() string {return "*** Ответ RCS высота: " + c.w + c.h + c.s}
func (c rcs) pe() string {return "*** Ответ RCS периметер : " + c.w + c.h}
func (c rcs) le() string {return "*** Ответ RCS площадь : " + c.h + c.l}
// **************************************************************************************************************************
// Here's a basic interface for geometric shapes.
type geometry interface {
area() float64
perim() float64
}
// For our example we'll implement this interface on
// `rect` and `circle` types.
type rect struct {width, height float64}
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.
func (r rect) area() float64 { return r.width * r.height}
func (r rect) perim() float64 { return 2*r.width + 2*r.height}
// The implementation for `circle`s.
func (c circle) area() float64 { return math.Pi * c.radius * c.radius}
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`.
func measure(g geometry) {
fmt.Println(g)
fmt.Println(g.area())
fmt.Println(g.perim())
}
func tss(c ry){
fmt.Println(c)
fmt.Println(c.ar())
fmt.Println(c.pe())
fmt.Println(c.le())
}
func main() {
r := rect{width: 3, height: 4}
c := circle{radius: 5}
z := circle{15}
// The `circle` and `rect` struct types both
// implement the `geometry` interface so we can use
// instances of
// these structs as arguments to `measure`.
measure(r)
measure(c)
measure(z)
fmt.Println("-----------------------------------------------------------------------")
f := rct{"Яблоко","Банна","Апельсин"}
tss(f)
l :=lct{"Глобус","Земля"}
tss(l)
b:=rcs{"Новости","Известия","Глава","Статья"}
tss(b)
m:=rcs{"Пример","Второй","Третий","Четветртый"}
tss(m)
if l==m{
fmt.Println("Совпадение")
}else{
fmt.Println("Нет совпадения")
}
}