2013-07-03 4 views
2

나는 An Introduction to Programming in Go을 통해 인터페이스를 파악하려고합니다. 나는 그들이 무엇인지, 왜 그것이 필요한지에 대한 좋은 생각을 갖고있는 것처럼 느껴지지만, 나는 그것을 사용하는 데 어려움을 겪고 있습니다. 섹션의 끝에서 자신이 갖고필드를 필드로 사용하기

인터페이스는 필드로 사용할 수 있습니다

func (m *MultiShape) area() float64 { 
    var area float64 
    for _, s := range m.shapes { 
     area += s.area() 
    } 
    return area 
} 
:

type MultiShape struct { 
    shapes []Shape 
} 

우리는 심지어에게 영역 방법을 제공하여 모양으로 MultiShape 자체를 설정할 수 있습니다

이제 MultiShape에는 원, 사각형 또는 다른 다중도가 포함될 수 있습니다.

나는 이것을 어떻게 사용하는지 모른다. 이것의 나의 이해는 MultiShape이 나는 ​​사람이 나를 도와 줄 수

package main 

import ("fmt"; "math") 


type Shape interface { 
    area() float64 
} 

type MultiShape struct { 
    shapes []Shape 
} 

func (m *MultiShape) area() float64 { 
    var area float64 
    for _, s := range m.shapes { 
     area += s.area() 
    } 
    return area 
} 

// =============================================== 
// Rectangles 

type Rectangle struct { 
    x1, y1, x2, y2 float64 
} 

func distance(x1, y1, x2, y2 float64) float64 { 
    a := x2 - x1 
    b := y2 - y1 
    return math.Sqrt(a*a + b*b) 
} 

func (r *Rectangle) area() float64 { 
    l := distance(r.x1, r.y1, r.x1, r.y2) 
    w := distance(r.x1, r.y1, r.x2, r.y1) 
    return l*w 
} 

// =============================================== 
// Circles 

type Circle struct { 
    x, y, r float64 
} 

func (c * Circle) area() float64 { 
    return math.Pi * c.r*c.r 
} 

// =============================================== 

func totalArea(shapes ...Shape) float64 { 
    var area float64 
    for _, s := range shapes { 
     area += s.area() 
    } 
    return area 
} 

func main() { 

    c := Circle{0,0,5} 
    fmt.Println(c.area()) 

    r := Rectangle{0, 0, 10, 10} 
    fmt.Println(r.area()) 

    fmt.Println(totalArea(&r, &c)) 

    //~ This doesn't work but this is my understanding of it 
    //~ m := []MultiShape{c, r} 
    //~ fmt.Println(totalArea(&m)) 

} 

와 예제 코드 일하고있다 그것의 CircleRectangleslice

있어 할 수 있습니까? 나는 파이썬 배경을 가지고 있으므로 도움이 될 두 종류의 링크가 있다면.

감사

예를 들어

답변

2

,

package main 

import (
    "fmt" 
    "math" 
) 

type Shape interface { 
    area() float64 
} 

type MultiShape struct { 
    shapes []Shape 
} 

func (m *MultiShape) area() float64 { 
    var area float64 
    for _, s := range m.shapes { 
     area += s.area() 
    } 
    return area 
} 

type Rectangle struct { 
    x1, y1, x2, y2 float64 
} 

func (s *Rectangle) area() float64 { 
    x := math.Abs(s.x2 - s.x1) 
    y := math.Abs(s.y2 - s.y1) 
    return x * y 
} 

func (s *Rectangle) distance() float64 { 
    x := s.x2 - s.x1 
    y := s.y2 - s.y1 
    return math.Sqrt(x*x + y*y) 
} 

type Circle struct { 
    x, y, r float64 
} 

func (s *Circle) area() float64 { 
    return math.Pi * s.r * s.r 
} 

func main() { 
    r1 := Rectangle{1, 1, 4, 4} 
    fmt.Println(r1.area()) 
    r2 := Rectangle{2, 4, 3, 6} 
    fmt.Println(r2.area()) 
    c := Circle{10, 10, 2} 
    fmt.Println(c.area()) 
    m := MultiShape{[]Shape{&r1, &r2, &c}} 
    fmt.Println(m.area()) 
} 

출력 :

9 
2 
12.566370614359172 
23.566370614359172 
+0

내가 많이, 나는하지 않는 부분은'MultiShape'이라고합니다. – Jeff

+0

: P 죄송합니다. 완료했다고 생각했습니다. – Jeff

+0

지금보십시오. – peterSO

관련 문제