golang 接口interface 09

叁歲伎倆 2022-12-10 07:27 323阅读 0赞

1.接口会自动和拥有相同方法的结构体关联

  1. /*
  2. 只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。
  3. 如果一个变量含有了多个interface类型的方法,那么这个变量就实现了多个接口。
  4. */
  5. type MyInterface interface{
  6. GetName() string
  7. GetAge() int
  8. }
  9. type StringInterface interface{
  10. SumAgeSa() int
  11. }
  12. type CommonInterface interface{
  13. MyInterface
  14. StringInterface
  15. GetGender() string
  16. }
  17. type Employee struct {
  18. name string
  19. age int
  20. salary int
  21. gender string
  22. }
  23. func (self *Employee) GetName() string {
  24. return self.name
  25. }
  26. func (self *Employee) GetAge() int {
  27. return self.age
  28. }
  29. func (self *Employee) SumAgeSa() int {
  30. return self.age + self.salary
  31. }
  32. func (self *Employee) GetGender() string {
  33. return self.gender
  34. }
  35. /*自动关联上了*/
  36. func IntRun() {
  37. // 空接口的使用,空接口类型的变量可以保存任何类型的值
  38. // 空格口类型的变量非常类似于弱类型语言中的变量
  39. var varEmptyInterface interface{}
  40. fmt.Println("varEmptyInterface is of type %T\n", varEmptyInterface)
  41. varEmptyInterface = 100
  42. y := varEmptyInterface.(int) //这里可以类型转换(类型断言 )
  43. fmt.Println("varEmptyInterface is of type int", varEmptyInterface, y)
  44. varEmptyInterface = "Golang"
  45. k, err := varEmptyInterface.(string) //待判断的类型转换(类型断言 )
  46. if err {
  47. fmt.Println("varEmptyInterface is of type string", varEmptyInterface, k)
  48. }
  49. varEmployee := Employee{
  50. name: "Jack Ma",
  51. age: 50,
  52. salary: 100000000,
  53. gender: "Male",
  54. }
  55. var mm MyInterface = &varEmployee
  56. fmt.Println(mm.GetName())
  57. fmt.Println(mm.GetAge())
  58. //这里可以调用组合内的所有接口
  59. var ww CommonInterface = &varEmployee
  60. fmt.Println(ww.GetAge(), ww.GetGender(), ww.GetName(), ww.SumAgeSa())
  61. }

2.通过类型获取值

  1. func IntRun() {
  2. var varEmptyInterface interface{}
  3. varEmptyInterface = 100
  4. y := varEmptyInterface.(int) //这里可以类型转换(类型断言 )
  5. fmt.Println("varEmptyInterface is of type int", varEmptyInterface, y)
  6. classifier(varEmptyInterface)
  7. }
  8. /*
  9. 通过类型获取值
  10. */
  11. func classifier(items ...interface{}) {
  12. for i, x := range items {
  13. switch x.(type) {
  14. case bool:
  15. fmt.Println("classifier", "bool", i, x)
  16. case float64:
  17. fmt.Println("classifier","float64", i, x)
  18. case int, int64:
  19. fmt.Println("classifier","int", i, x)
  20. case nil:
  21. fmt.Println("classifier","nil", i, x)
  22. case string:
  23. fmt.Println("classifier","string", i, x)
  24. default:
  25. fmt.Println("classifier","other", i, x)
  26. }
  27. }
  28. }

3.简单工厂

  1. /*
  2. 简单工厂例子
  3. */
  4. type Animal interface {
  5. Sleep()
  6. Age() int
  7. Type() string
  8. }
  9. type Cat struct {
  10. MaxAge int
  11. }
  12. func (this *Cat) Sleep() {
  13. fmt.Println("Cat need sleep")
  14. }
  15. func (this *Cat) Age() int {
  16. return this.MaxAge
  17. }
  18. func (this *Cat) Type() string {
  19. return "Cat"
  20. }
  21. type Dog struct {
  22. MaxAge int
  23. }
  24. func (this *Dog) Sleep() {
  25. fmt.Println("Dog need sleep")
  26. }
  27. func (this *Dog) Age() int {
  28. return this.MaxAge
  29. }
  30. func (this *Dog) Type() string {
  31. return "Dog"
  32. }
  33. func Factory(name string) Animal {
  34. switch name {
  35. case "dog":
  36. return &Dog{MaxAge: 20}
  37. case "cat":
  38. return &Cat{MaxAge: 10}
  39. default:
  40. panic("No such animal")
  41. }
  42. }

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 golang 接口interface 09

     1.接口会自动和拥有相同方法的结构体关联 / 只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。 如果一个变量含有了多个int

    相关 interface接口

    当类中的方法都是抽象方法, 接口格式特点: 1、接口中可以定义常量和抽象方法。 2、接口中成员有固定修饰符:       常量:public static final 

    相关 golang:空接口类型(interface{})

    空接口是接口类型的特殊形式。空接口没有任何方法,因此任何类型都无须实现空接口。从实现的家督来看,任何值都满足这个接口的需求。因此空接口类型可以保存任何值,也可以从空接口中取出原

    相关 012.golang 接口interface

    接口interface 接口是一个或多个方法签名的集合 只要某个类型拥有该接口的所有方法签名,即算实现该接口,无需显示 声明实现了哪个接口,这称为 St

    相关 接口interface

    什么是接口? 一种把类抽象的更彻底,接口里只能包含抽象方法的“特殊类”。接口不关心类的内部状态数据,定义的是一批类所遵守的规范。(它只规定这批类里必须提供某些方法,提供这