Last active
June 15, 2022 18:45
-
-
Save Yourun-proger/cb498ed6d66719dead1c4ff5aa99c61b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import "fmt" | |
//-------------------- ABSTRACT INTERFACES -------------------- | |
type WaterAbsorber interface { | |
Pour(num int) | |
TotalLiquid() int | |
} | |
type WaterSource interface { | |
WaterAbsorber | |
PourOut() | |
} | |
//-------------------- CONCRECT CLASSES -------------------------- | |
type Human struct { | |
liquid int | |
} | |
func (h Human) TotalLiquid() int { | |
return h.liquid | |
} | |
func (h *Human) Pour(num int) { | |
h.liquid += num | |
} | |
type Cat struct { | |
liquid int | |
} | |
func (c Cat) TotalLiquid() int { | |
return c.liquid | |
} | |
func (c *Cat) Pour(num int) { | |
c.liquid += num | |
} | |
type Cup struct { | |
liquid int | |
} | |
func (c Cup) TotalLiquid() int { | |
return c.liquid | |
} | |
func (c *Cup) Pour(num int) { | |
c.liquid += num | |
} | |
func (c *Cup) PourOut() { | |
c.liquid = 0 | |
} | |
//-------------------- IMPLEMENTATION THE ABILITY TO "DRINK" FOR CLASS INSTANCES -------------------------- | |
func drink(wa WaterAbsorber, ws WaterSource) { | |
wa.Pour(ws.TotalLiquid()) | |
ws.PourOut() | |
} | |
//-------------------- MAIN -------------------------- | |
func main() { | |
human := &Human{4} | |
cup := &Cup{7} | |
drink(human, cup) | |
fmt.Println("Human liquid level: ", human.TotalLiquid()) | |
fmt.Println("Cup liquid level: ", cup.TotalLiquid()) | |
fmt.Println("____________________________________") | |
new_cup := &Cup{23} | |
drink(cup, new_cup) | |
fmt.Println("Cup liquid level: ", cup.TotalLiquid()) | |
fmt.Println("New cup liquid level: ", new_cup.TotalLiquid()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment