daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest Dec 19th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type plant1 struct {
  8.     name   string
  9.     height int
  10. }
  11.  
  12. type plant2 struct {
  13.     species string
  14.     height  int
  15. }
  16.  
  17. func (self *plant1) grow() {
  18.     self.height++
  19. }
  20. func (self *plant2) grow() {
  21.     self.height++
  22. }
  23.  
  24. func (self plant1) getHeight() int {
  25.     return self.height
  26. }
  27. func (self plant2) getHeight() int {
  28.     return self.height
  29. }
  30.  
  31. type plant interface {
  32.     getHeight() int
  33.     //grow()
  34. }
  35.  
  36. func showHeight(p plant) {
  37.     switch p.(type) {
  38.     case plant1:
  39.         fmt.Println(p.(plant1).name, `Height = `, p.(plant1).getHeight())
  40.     case plant2:
  41.         fmt.Println(p.(plant2).species, `Height = `, p.(plant2).getHeight())
  42.     }
  43.  
  44. }
  45.  
  46. func main() {
  47.  
  48.     p1 := plant1{
  49.         name:   `Plant 10`,
  50.         height: 1,
  51.     }
  52.     p2 := plant2{
  53.         species:   `Plant 20`,
  54.         height: 1,
  55.     }
  56.     p1.grow()
  57.     p1.grow()
  58.     p2.grow()  
  59.  
  60.     showHeight(p1)
  61.     showHeight(p2)
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top