接口对象的转型有两种方式:

1. 方式一:instance,ok:=接口对象.(实际类型)

  如果该接口对象是对应的实际类型,那么instance就是转型之后对象,ok的值为true
  配合if...else if...使用

2. 方式二:

  接口对象.(type)
  配合switch...case语句使用

示例:

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math"
  6. )
  7.  
  8. type shape interface {
  9. perimeter() int
  10. area() int
  11. }
  12.  
  13. type rectangle struct {
  14. a int // 长
  15. b int // 宽
  16. }
  17. func (r rectangle) perimeter() int {
  18. return (r.a + r.b) * 2
  19. }
  20. func (r rectangle) area() int {
  21. return r.a * r.b
  22. }
  23.  
  24. type circle struct {
  25. radios int
  26. }
  27. func (c circle) perimeter() int {
  28. return 2 * c.radios * int(math.Round(math.Pi))
  29. }
  30. func (c circle) area() int {
  31. return int(math.Round(math.Pow(float64(c.radios), 2) * math.Pi))
  32. }
  33.  
  34. func getType(s shape) {
  35. if i, ok := s.(rectangle); ok {
  36. fmt.Printf("长方形的长:%d,长方形的宽是:%d\n", i.a, i.b)
  37. } else if i, ok := s.(circle); ok {
  38. fmt.Printf("圆形的半径是:%d\n", i.radios)
  39. }
  40. }
  41.  
  42. func getType2(s shape) {
  43. switch i := s.(type) {
  44. case rectangle:
  45. fmt.Printf("长方形的长:%d,长方形的宽是:%d\n", i.a, i.b)
  46. case circle:
  47. fmt.Printf("圆形的半径是:%d\n", i.radios)
  48. }
  49. }
  50.  
  51. func getResult(s shape) {
  52. fmt.Printf("图形的周长是:%d,图形的面积是:%d\n", s.perimeter(), s.area())
  53. }
  54.  
  55. func main() {
  56. r := rectangle{a: 10, b: 20}
  57. getType(r)
  58. getResult(r)
  59.  
  60. c := circle{radios: 5}
  61. getType2(c)
  62. getResult(c)
  63. }

  

golang中接口对象的转型的更多相关文章

  1. 转载 - java中接口的向上转型。和多态性

    发现一篇对接口总结很精简的文章 1.在java中接口就是一个完全抽象的类,跟抽象类一样不能产生对象,但是可以作为对象的引用,可以由其实现类向上转型,它就跟超类一样, 向上转型了,可以很好的利用接口,可 ...

  2. golang中接口interface和struct结构类的分析

    再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和 ...

  3. 七、golang中接口、反射

    一.接口定义 1.定义 interface类型可以定义一组方法,但是这些不需要实现,并且interface不能包含任何变量 package main import ( "fmt" ...

  4. golang中接口详解

    package main import ( "fmt" ) type Sayer interface { say() } type Mover interface { move() ...

  5. golang中接口类型小案例

    1.  在项目中实现注册成功之后,向用户发送邮件.微信提醒 package main import "fmt" type IMessage interface { send() b ...

  6. golang(08)接口介绍

    原文链接 http://www.limerence2017.com/2019/09/12/golang13/#more 接口简介 golang 中接口是常用的数据结构,接口可以实现like的功能.什么 ...

  7. Golang 中的 面向对象: 方法, 类, 方法继承, 接口, 多态的简单描述与实现

    前言: Golang 相似与C语言, 基础语法与C基本一致,除了广受争议的 左花括号 必须与代码同行的问题, 别的基本差不多; 学会了C, 基本上万变不离其宗, 现在的高级语言身上都能看到C的影子; ...

  8. xlua中lua对象到c#对象的转型

    lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...

  9. 关于Java中的对象、类、抽象类、接口、继承之间的联系

    关于Java中的对象.类.抽象类.接口.继承之间的联系: 导读: 寒假学习JavaSE基础,其中的概念属实比较多,关联性也比较大,再次将相关的知识点复习一些,并理顺其中的关系. 正文: 举个例子:如果 ...

随机推荐

  1. JAVA微信公众号网页开发——生成自定义微信菜单(携带参数)

    官网接口地址:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.ht ...

  2. libevent之学习文档

    1.官方文档 2.中文版 3.gitbook

  3. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  4. MacOS使用Docker创建MySQL主从数据库

    一.拉取MySQL镜像 通过终端获取最新的MySQL镜像 docker pull mysql/mysql-server 二.创建MySQL数据库容器配置文件对应目录 我们在当前用户下创建一组目录,用来 ...

  5. POJ 3126:Prime Path(素数+BFS)

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  6. StringBoot

    1.首先我们需要依赖SpringBoot父工程,这是每个项目中必须要有的. <!--引入SpringBoot父依赖--><parent>        <groupId& ...

  7. Momentum and NAG

    目录 Momentum Nesterov accelerated gradient NESTEROV 的另外一个方法? Momentum Momentum的迭代公式为: \[v_t = \gamma ...

  8. CS229 机器学习课程复习材料-线性代数

    本文是斯坦福大学CS 229机器学习课程的基础材料,原始文件下载 原文作者:Zico Kolter,修改:Chuong Do, Tengyu Ma 翻译:黄海广 备注:请关注github的更新,线性代 ...

  9. 初遇NFT-IPFS

    初遇NFT-IPFS 本次学习如何使用Hardhat框架制作可预售的NFT并利用IPFS存储元数据. NFT简介 NFT全称Non-fungible Token(即非同质化通证).不可分割性(目前有碎 ...

  10. MATLAB/SIMULINK生成代码错误之change the default character encoding setting

    SIMULINK点击生成C代码报错 错误提示: Error encountered while executing PostCodeGenCommand for model 'RTW_sc3': Cl ...