Girlfriend

AUTHOR: Locez
VERSION: 1

本文并非正经博客,just a girlfriend.在本文,你将看到 go 语言是如何定义变量,类型,使用“继承”,其次还会看到如何将函数绑定到对象上,实现我们平时所说的面向对象编程中的 . 调用。当然因为不是正经博客,所以本文不会讲解语法,你看完只会收获一个女朋友(开心么?兴奋么?),没有任何讲解。

看看我们的最终结果:

1
2
3
4
5
6
7
I walk 520 steps to meet you.
I really want to tell you my name, but I have forgotten it.
it is hard to believe it?
don't say anything, just kiss me.
your arms are really warm.
event if I am a bunch of code, I have no entity, can't leave the screen.
but I will stay with you forever, I love you.

the code here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import "fmt"

func main() {
gf := &girlfriend{
people: people{Name: "",
HairColor: "black",
Age: 18,
Height: 160.00,
Weight: 45.00},
Sex: "female",
}
gf.walk(520)
fmt.Printf("%v\n", gf)
gf.say("it is hard to believe it?")
gf.kiss()
gf.hug()
gf.confess()
}

type people struct {
Name string
HairColor string
Age int
Height float64
Weight float64
}

type girlfriend struct {
people
Sex string
}

func (p *people) String() string {
if p.Name == "" {
return "I really want to tell you my name, but I have forgotten it."
}
return fmt.Sprintf("my name is %v", p.Name)
}

func (p *people) walk(n int) {
fmt.Printf("I walk %v steps to meet you.\n", n)
}

func (p *people) say(s string) {
fmt.Printf("%v\n", s)
}

func (g *girlfriend) kiss() {
fmt.Println("don't say anything, just kiss me.")
}

func (g *girlfriend) hug() {
fmt.Println("your arms are really warm.")
}

func (g *girlfriend) confess() {
fmt.Println("event if I am a bunch of code, I have no entity, can't leave the screen.")
fmt.Println("but I will stay with you forever, I love you.")
}