Skip to content

Instantly share code, notes, and snippets.

@Yourun-proger
Last active June 15, 2022 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yourun-proger/cb498ed6d66719dead1c4ff5aa99c61b to your computer and use it in GitHub Desktop.
Save Yourun-proger/cb498ed6d66719dead1c4ff5aa99c61b to your computer and use it in GitHub Desktop.
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