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

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
美团技术团队
NISL@THU
NISL@THU
C
Cisco Blogs
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Cloudbric
Cloudbric
雷峰网
雷峰网
T
Tailwind CSS Blog
博客园 - 司徒正美
The Register - Security
The Register - Security
L
LangChain Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threat Research - Cisco Blogs
I
InfoQ
S
Schneier on Security
L
Lohrmann on Cybersecurity
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
D
Docker
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Hacker News: Front Page
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Webroot Blog
Webroot Blog
MongoDB | Blog
MongoDB | Blog

博客园 - 远洪

大模型常用术语 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)
}

运行结果: