Skip to content

Commit 4e68a60

Browse files
committed
Ders 41
1 parent 0cb2686 commit 4e68a60

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

41_polymorphism/main.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type shape interface {
6+
area() float64
7+
}
8+
9+
func printArea(shapes ...shape) {
10+
for _, shape := range shapes {
11+
fmt.Println("Alan : ", shape.area())
12+
}
13+
}
14+
15+
type triangle struct {
16+
a float64
17+
h float64
18+
}
19+
20+
func (t triangle) area() float64 {
21+
return (t.a * t.h) / 2
22+
}
23+
24+
type square struct {
25+
a float64
26+
}
27+
28+
func (s square) area() float64 {
29+
return (s.a * s.a)
30+
}
31+
32+
type rectangle struct {
33+
a, b float64
34+
}
35+
36+
func (r rectangle) area() float64 {
37+
return (r.a * r.b)
38+
}
39+
40+
func main() {
41+
42+
t := triangle{3, 4}
43+
s := square{4}
44+
r := rectangle{4, 5}
45+
46+
printArea(t, s, r)
47+
48+
}

0 commit comments

Comments
 (0)