Created
February 2, 2022 09:52
-
-
Save ihsanbudiman/e38975021a2308dbe5447bbc1d3da4ac to your computer and use it in GitHub Desktop.
openclosed in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BangunDatar interface { | |
Luas() float64 | |
} | |
type Persegi struct { | |
Sisi float64 | |
} | |
func (p Persegi) Luas() float64 { | |
return p.Sisi * p.Sisi | |
} | |
type Segitiga struct { | |
Alas float64 | |
Tinggi float64 | |
} | |
func (s Segitiga) Luas() float64 { | |
return (s.Alas * s.Tinggi) / 2 | |
} | |
type Kalkulator struct{} | |
func (k Kalkulator) TotalLuas(daftarBenda ...BangunDatar) float64 { | |
totalLuas := 0.0 | |
for _, benda := range daftarBenda { | |
totalLuas += benda.Luas() | |
} | |
return totalLuas | |
} | |
func main() { | |
p := Persegi{4} | |
s := Segitiga{3, 4} | |
k := Kalkulator{} | |
fmt.Println(k.TotalLuas(p, s)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment