We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0cb2686 commit 4e68a60Copy full SHA for 4e68a60
41_polymorphism/main.go
@@ -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
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