開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Go (プログラミング言語)
- Introducing Go: Build Reliable, Scalable Programs Caleb Doxsey (著) (参考書籍)
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の10章(継承とポリモーフィズム)、11.9(練習問題)、問題11-1.をGoで取り組んでみる。
コード(Emacs)
package main
import "fmt"
type sound interface {
ring()
}
type telephone struct {
phonetype string
}
func (t telephone) ring() {
fmt.Printf("Ringing the %s\n", t.phonetype)
}
type electronicPhone struct {
}
func (t electronicPhone) ring() {
fmt.Println("Ringing the Digital")
}
func main() {
t := telephone{"tel"}
ep := electronicPhone{}
ts := []sound{t, ep}
for _, x := range ts {
x.ring()
}
}
入出力結果(Terminal)
$ go run sample1.go Ringing the tel Ringing the Digital $
0 コメント:
コメントを投稿