企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 示例 ### 音乐播放器适配 <details> <summary>main.go</summary> ``` package main import "fmt" // 播放接口 type Player interface { PlayMusic() } // 音乐播放器 type MusicPlayer struct { Src string } func (p MusicPlayer) PlayMusic() { fmt.Println("play music: " + p.Src) } // 游戏音乐播放器 type GameSoundPlayer struct { Src string } func (p GameSoundPlayer) PlaySound() { fmt.Println("play sound: " + p.Src) } // 使游戏音乐播放器适配音乐播放器 type GameSoundPlayerAdapter struct{ SoundPlayer GameSoundPlayer } func (g GameSoundPlayerAdapter) PlayMusic(){ g.SoundPlayer.PlaySound() } func main() { player :=GameSoundPlayer{Src:"game.mp3"} playerApater := GameSoundPlayerAdapter {SoundPlayer:player} play(playerApater) } func play(player Player) { player.PlayMusic() } ``` </details> <br /> 输出 ``` play sound: game.mp3 ```