type 接口名字 interface {
方法声明
}
package main
import "fmt"
type Personer interface {
SayHello()
}
type Student struct {
}
func (s *Student) SayHello(){
fmt.Println("老师好")
}
type Teacher struct {
}
func (t *Teacher) SayHello(){
fmt.Println("学生好")
}
func main() {
var stu Student
var teacher Teacher
var person Personer
person = &stu
person.SayHello()
person = &teacher
person.SayHello()
}
func 函数名 (参数 接口类型){
}
package main
import "fmt"
type Personer interface {
SayHello()
}
type Student struct {
}
func (s *Student) SayHello(){
fmt.Println("老师好")
}
type Teacher struct {
}
func (t *Teacher) SayHello(){
fmt.Println("学生好")
}
func WhoSayHi(h Personer) {
h.SayHello()
}
func main() {
var stu Student
var teacher Teacher
WhoSayHi(&stu)
WhoSayHi(&teacher)
}
package main
import "fmt"
type Stroager interface {
Read()
Write()
}
// 移动硬盘
type MDisk struct {
}
func (m *MDisk) Read() {
fmt.Println("移动硬盘读取数据")
}
func (m *MDisk) Write() {
fmt.Println("移动硬盘写入数据")
}
// U盘
type UDisk struct {
}
func (m *UDisk) Read() {
fmt.Println("U盘读取数据")
}
func (m *UDisk) Write() {
fmt.Println("U盘写入数据")
}
func Computer(c Stroager) {
c.Read()
c.Write()
}
func main() {
var mdisk MDisk
var udisk UDisk
Computer(&mdisk)
Computer(&udisk)
}
package main
import "fmt"
type Object struct {
numA int
numB int
}
type Resulter interface {
GetResult() int
}
type Add struct {
Object
}
type Sub struct {
Object
}
func (a *Add) GetResult() int {
return a.numA + a.numB
}
func (s *Sub) GetResult() int {
return s.numA - s.numB
}
// 假设需求修改,从原先+的需求修改为-
// 如果使用原来的方法,需要将Add创建对象的方法删除掉,修改为Sub的方法
// 现在希望在main中尽量少的修改就完成需求实现
//1、定义一个新的类
//2、创建一个方法,在该方法中完成对象的创建
type OperatorFactory struct {
}
func (o *OperatorFactory) CreateOperator(op string, numA int, numB int) int {
switch op {
case "+":
add := Add{Object{numA: numA, numB: numB}}
return OperatorWho(&add)
case "-":
sub := Sub{Object{numA: numA, numB: numB}}
return OperatorWho(&sub)
default:
return 0
}
}
func OperatorWho(h Resulter) int {
return h.GetResult()
}
func main() {
var operator OperatorFactory
num := operator.CreateOperator("-", 10, 5)
fmt.Println(num)
}
package main
import "fmt"
type Humaner interface {
SayHello()
}
type Personer interface {
Humaner
Say()
}
type Student struct {
}
func (s *Student) SayHello() {
fmt.Println("大家好")
}
func (s *Student) Say() {
fmt.Println("你好")
}
func main() {
var stu Student
var per Personer
per = &stu
per.Say()
per.SayHello() // 可以调用所继承的接口中的方法
}
func main() {
var stu Student
var per Personer
per = &stu
var h Humaner
h = per
h.SayHello()
//h.Say() 这个是无法调用的
}
func main() {
var i interface{}
i = 123
fmt.Println(i)
i = "abc"
fmt.Println(i)
//可以存储各种类型数据的切片
var s []interface{}
s = append(s, 123, "abc", 12.3)
for _, value := range s {
fmt.Println(value)
}
}
func main() {
var i interface{}
i = 123
i = "abc"
value, ok := i.(int)
if ok {
fmt.Println(value)
} else {
fmt.Println("类型推断错误")
}
}
package main
import "fmt"
type Object struct {
numA int
numB int
}
type Resulter interface {
GetResult() int
SetData(data ...interface{}) bool // 完成参数运算的数据类型的校验
}
type Add struct {
Object
}
type Sub struct {
Object
}
func (a *Add) GetResult() int {
return a.numA + a.numB
}
func (a *Add) SetData(data ...interface{}) bool {
var b bool = true
//对数据个数进行校验
if len(data) > 2 {
b = false
}
//类型校验
value1, ok1 := data[0].(int)
if !ok1 {
fmt.Println("第一个数类型错误")
b = false
}
value2, ok2 := data[1].(int)
if !ok2 {
fmt.Println("第二个数类型错误")
b = false
}
a.numA = value1
a.numB = value2
return b
}
func (s *Sub) GetResult() int {
return s.numA - s.numB
}
func (s *Sub) SetData(data ...interface{}) bool {
var b bool = true
//对数据个数进行校验
if len(data) > 2 {
b = false
}
//类型校验
value1, ok1 := data[0].(int)
if !ok1 {
fmt.Println("第一个数类型错误")
b = false
}
value2, ok2 := data[1].(int)
if !ok2 {
fmt.Println("第二个数类型错误")
b = false
}
s.numA = value1
s.numB = value2
return b
}
// 假设需求修改,从原先+的需求修改为-
// 如果使用原来的方法,需要将Add创建对象的方法删除掉,修改为Sub的方法
// 现在希望在main中尽量少的修改就完成需求实现
//1、定义一个新的类
//2、创建一个方法,在该方法中完成对象的创建
type OperatorFactory struct {
}
func (o *OperatorFactory) CreateOperator(op string) Resulter {
switch op {
case "+":
add := new(Add)
return add
case "-":
sub := new(Sub)
return sub
default:
return nil
}
}
func OperatorWho(h Resulter) int {
return h.GetResult()
}
func main() {
var operator OperatorFactory
obj := operator.CreateOperator("-")
b := obj.SetData(10, 20)
if b {
num := OperatorWho(obj)
fmt.Println(num)
}
}