惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
G
Google Developers Blog
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
博客园 - 叶小钗
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
P
Palo Alto Networks Blog
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
Security Latest
Security Latest
H
Heimdal Security Blog
小众软件
小众软件
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
P
Privacy International News Feed
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
Scott Helme
Scott Helme
博客园 - 【当耐特】
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
Jina AI
Jina AI
Spread Privacy
Spread Privacy
量子位
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网

博客园 - 远洪

大模型常用术语 windows 使用sshAgent 加载秘钥 再次认识java反射 再次认识java注解 再次认识java泛型 java中类的分类 java类中的成员变量,静态变量与局部变量 再谈java枚举enum 使用CCProxy让手机访问电脑能访问的网址 playwright启动后报错net::ERR_CERT_COMMON_NAME_INVALID 解决方法 debian 或ubuntu安装使用tigervnc python 实例属性、类属性、实例方法、类方法、静态方法 python面向对象封装,私有变量 docker compose使用 docker 自定义网络 Dockerfile 使用 golang进程(主线程)与协程 java中的多态与golang中的多态 golang 定义接口
go语言多态中的类型断言
远洪 · 2024-01-12 · via 博客园 - 远洪

类型断言案例

package main

import (
    "fmt"
)

type Usb interface{
    Connect()
    DisConnect()
}

type Phone struct{
    Name string
}

/*
*  Phone实现了Usb 接口(是指实现了Usb接口的所有方法)
*/
func(p Phone) Connect(){
    fmt.Println("手机连接...")
}

func(p Phone) DisConnect(){
    fmt.Println("手机断开连接...")
}
// Phone 类中多了一个 Call 方法
func(p Phone) Call(){
    fmt.Println("手机打电话")
}

type Camera struct{
    Name string
}

func(c Camera) Connect(){
    fmt.Println("相机连接...")
}

func(c Camera) DisConnect(){
    fmt.Println("相机断开连接...")
}

type  Computer struct{
}

func(c Computer) Working(u Usb){     //  这里就提现了一个多态的实现 (①里提现为多态参数)
    u.Connect()

    // 这里使用了类型断言,当 u 为Phone 的时候,可以调用 Call方法
    if phone, ok := u.(Phone); ok == true {
        phone.Call()
    }

    u.DisConnect()
}

func main(){
    phone := Phone{"手机"}
    camera := Camera{"相机"}
    computer := Computer{}
    computer.Working(phone)    
    computer.Working(camera)

    var ar [2]Usb     // 多态数组的提现 ar 中放了两个不同的结构体
    ar[0] = phone
    ar[1] = camera
    fmt.Println(ar)
}