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

推荐订阅源

H
Heimdal Security Blog
A
Arctic Wolf
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs
D
Docker
爱范儿
爱范儿
T
Tenable Blog
C
Check Point Blog
B
Blog
C
Cisco Blogs
Vercel News
Vercel News
The Cloudflare Blog
T
Threatpost
NISL@THU
NISL@THU
T
Tor Project blog
V2EX - 技术
V2EX - 技术
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
S
Security @ Cisco Blogs
GbyAI
GbyAI
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
Y
Y Combinator Blog
博客园_首页
T
Troy Hunt's Blog
The Hacker News
The Hacker News
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
U
Unit 42
AWS News Blog
AWS News Blog
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 远洪

大模型常用术语 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进程(主线程)与协程 go语言多态中的类型断言 golang 定义接口
java中的多态与golang中的多态
远洪 · 2024-01-12 · via 博客园 - 远洪

多态是面向对象编程的一个重要特征。它允许不同类型的对象对同一方法进行不同的实现。具体来说,多态性指的是通过父类的引用变量来引用子类的对象,从而实现对不同对象的统一操作。

java中多态通过继承实现,在go语言中,多态通过接口实现

java多态案例

package org.example;

class Animal{
    public void sound(){
        System.out.println("动物发出叫声");
    }
}

class Cat extends  Animal{
    @Override
    public void sound() {
        System.out.println("猫叫");
    }
}
class Dog extends Animal{
    @Override
    public void sound() {
        System.out.println("狗叫");
    }
}

public class App 
{
    public static void main( String[] args )
    {
        Animal cat = new Cat();    // 父类引用指向子类对象
        Animal dog = new Dog();    // 父类引用指向子类对象
        cat.sound();
        dog.sound();
    }
}

运行结果:

 go多态案例

go语言中,多态可以体现为多态参数和多态数组,例如:

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("手机断开连接...")
}

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.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)
}

运行结果: