package main
import (
"fmt"
"math"
)
//interface可以实现泛型编程
//简单的说,interface是一组method的组合
//interface 可以实现泛型编程
type geometry interface {
calculatearea() float64
calculateperim() float64
}
type rectancle struct {
width, height float64
}
type circle struct {
radius float64
}
func (r rectancle) calculatearea() float64 {
return r.height * r.width
}
func (r rectancle) calculateperim() float64 {
return 2*r.width + 2*r.height
}
func (c circle) calculatearea() float64 {
return math.Pi * c.radius * c.radius
}
func (c circle) calculateperim() float64 {
return 2 * math.Pi * c.radius
}
func measure(g geometry) {
fmt.Println(g)
fmt.Println(g.calculatearea())
fmt.Println(g.calculateperim())
}
type Human struct {
name string
age int
phone string
}
type Student struct {
Human //匿名字段Human
school string
loan float32
}
type Employee struct {
Human //匿名字段Human
company string
money float32
}
//Human对象实现Sayhi方法
func (h Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}
// Human对象实现Sing方法
func (h Human) Sing(lyrics string) {
fmt.Println("La la, la la la, la la la la la...", lyrics)
}
//Human对象实现Guzzle方法
func (h Human) Guzzle(beerStein string) {
fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
}
// Employee重载Human的Sayhi方法
func (e Employee) SayHi() {
fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
e.company, e.phone) //此句可以分成多行
}
//Student实现BorrowMoney方法
func (s Student) BorrowMoney(amount float32) {
s.loan += amount // (again and again and...)
}
//Employee实现SpendSalary方法
func (e Employee) SpendSalary(amount float32) {
e.money -= amount // More vodka please!!! Get me through the day!
}
//nterface可以被任意的对象实现。我们看到上面的Men interface被Human、Student和Employee实现。
//同理,一个对象可以实现任意多个interface,例如上面的Student实现了Men和YoungChap两个interface。
type Men interface {
SayHi()
Sing(lyrics string)
Guzzle(beerStein string)
}
type YongChap interface {
SayHi()
string(song string)
BorrowMoney(amount float32)
}
type ElderlyGent interface {
SayHi()
Sing(song string)
SpendSalary(amount float32)
}
//嵌入inteface
//如果一个interface1作为interface2的一个嵌入字段,
//那么interface2隐式的包含了interface1里面的method。
type xiangbudaomingzile interface {
qianru
PrintWH()
}
type qianru interface {
MeasureArea()
}
type Rectancle2 struct {
width, height float32
}
func (r Rectancle2) PrintWH() {
fmt.Printf("This rectancle width is % height is %\n", r.width, r.height)
}
func (r Rectancle2) MeasureArea() {
fmt.Printf("This rectancle area is %\n", r.width*r.height)
}
type name interface {
WriteName()
}
type qianruname struct {
name
}
func (n qianruname) WriteName() {
fmt.Println("your name")
}
//练习一下interface
type isuibian interface {
SaySuibian()
}
type ssuibian struct {
s string
}
func (s ssuibian) SaySuibian() {
fmt.Println(s.s)
}
func lianxi(s isuibian) {
s.SaySuibian()
}
func main() {
suibian := ssuibian{s: "再练习一下interface"}
lianxi(suibian)
var n = qianruname{}
n.WriteName()
r := rectancle{10, 20}
c := circle{10}
measure(r)
measure(c)
mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}
paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}
sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}
Tom := Employee{Human{"Tom", 37, "222-444-XXX"}, "Things Ltd.", 5000}
//定义Men类型的变量i
var i Men
//i能存储Student
i = mike
fmt.Println("This is Mike, a Student:")
i.SayHi()
i.Sing("November rain")
//i也能存储Employee
i = Tom
fmt.Println("This is Tom, an Employee:")
i.SayHi()
i.Sing("Born to be wild")
//定义了slice Men
fmt.Println("Let's use a slice of Men and see what happens")
x := make([]Men, 3)
//这三个都是不同类型的元素,但是他们实现了interface同一个接口
x[0], x[1], x[2] = paul, sam, mike
for _, value := range x {
value.SayHi()
}
//空interface在我们需要存储任意类型的数值的时候相当有用,
//因为它可以存储任意类型的数值。它有点类似于C语言的void*类型。
// 定义a为空接口
var a interface{}
var i32 int = 5
s := "Hello world"
// a可以存储任意类型的数值
a = i32
println("a存储int型为:", a)
a = s
println("a存储sring类型为:", a)
var xian xiangbudaomingzile
r2 := Rectancle2{10, 10}
xian = r2
xian.PrintWH()
xian.MeasureArea()
}