1.接口
1.1接口实现
package main
import "fmt"
type Human interface {
sayhi()
}
type student struct {
name string
id int
}
func (s *student) sayhi() {
fmt.Println(s.name, s.id)
}
type teacher struct {
name string
id int
}
func (t *teacher) sayhi() {
fmt.Println(t.name, t.id)
}
func main() {
var i Human
var s student = student{"lili", 1}
i = &s
i.sayhi()
var t teacher = teacher{"bobo", 2}
i=&t
i.sayhi()
}
//输出结果
//lili 1
//bobo 2
1.2多态
接口作用实现多态。
package main
import "fmt"
type Human interface {
sayhi()
}
type student struct {
name string
id int
}
func (s *student) sayhi() {
fmt.Println("654321",s.name, s.id)
}
type teacher struct {
name string
id int
}
func (t *teacher) sayhi() {
fmt.Println("123456",t.name, t.id)
}
func who(i Human){
i.sayhi()
}
func main() {
var s student = student{"lili", 1}
who(&s)
var t teacher = teacher{"bobo", 2}
who(&t)
}
//输出结果
//654321 lili 1
//123456 bobo 2
一个函数who可以调用不同的两种方法。
1.3接口实例
接下来让我们用一个计算机的实例来介看看接口的用处!
1)使用面向对象的思想实现一个加减功能的计算器
package main
import "fmt"
type opertion struct {
}
func (o *opertion) getresult(info string, num1 float64, num2 float64) (result float64) {
switch info {
case "+":
result = num1 + num2
case "-":
result = num1 - num2
}
return
}
func main() {
var a opertion
result1:=a.getresult("+",2,4)
fmt.Println(result1)
return2:=a.getresult("-",5,2)
fmt.Println(return2)
}
//输出结果
//6
//3
我们定义了一个类(结构体),然后为该类创建了一个方法,封装了整个计算器功能,以后要使用直接使用该类(结构体)创建对象就可以了。这就是面向对象总的封装性。
2)用接口实现
package main
import "fmt"
type opertion struct {
num1 float64
num2 float64
}
type getresult interface {
getresult()float64
}
type Add struct {
opertion
}
func (a *Add) getresult() float64 {
return a.num1+a.num2
}
type Sub struct{
opertion
}
func (s *Sub) getresult () float64{
return s.num1-s.num2
}
type factory struct {
}
func oper(i getresult)float64{
return i.getresult()
}
func(f *factory)createfaction(info string,num1 float64,num2 float64)float64 {
var result float64
switch info {
case "+":
add := &Add{opertion{num1, num2}}
result=oper(add)
case "-":
sub := &Sub{opertion{num1, num2}}
result=oper(sub)
}
return result
}
func main() {
var factory factory
s:=factory.createfaction("+",10,20)
fmt.Println(s)
a:=factory.createfaction("-",30,20)
fmt.Println(a)
}
//输出结果
//30
//10
1.4接口继承与转换
package main
import "fmt"
type Humaner interface {
sayhi()
}
type Person interface {
Humaner
sing(s string)
}
type student struct {
name string
id int
}
func (s *student)sayhi() {
fmt.Println("hello")
}
func (sa *student)sing(s string) {
fmt.Println("student 在唱着",s)
}
func main() {
var i Person
s:=&student{"lili",11}
i=s
i.sayhi()
i.sing("哥哥")
}
//输出结果
//hello
//student 在唱着 哥哥
接口继承后,可以实现“超集”接口转换“子集”接口,代码如下:
package main
import "fmt"
type Humaner interface {
sayhi()
}
type Person interface {
Humaner
sing(s string)
}
type student struct {
name string
id int
}
func (s *student) sayhi() {
fmt.Println("hello")
}
func (sa *student) sing(s string) {
fmt.Println("student 在唱着", s)
}
func main() {
var i Person
s := &student{"lili", 11}
i = s
var m Humaner
//i=m //err
m=s
m.sayhi()
i.sing("哥哥")
}
//输出结果
//hello
//student 在唱着 哥哥
1.5空接口
空接口(interface{})不包含任何的方法,正因为如此,所有的类型都实现了空接口,因此空接口可以存储任意类型的数值。
例如:
package main
import "fmt"
func main() {
var i interface{}=1
fmt.Println("i=",i)
i="abc"
fmt.Println("i=",i)
}
//输出结果
//i= 1
//i= abc