Go言語 インターフェース

package main

import "fmt"

type englishScoreType int
type mathScoreType int

func (e englishScoreType) isPassed() bool {
    return e >= 80
}

func (m mathScoreType) isPassed() bool {
    return m >= 70
}

type passChecker interface {
    isPassed() bool
}

func showResult(score passChecker) {
    if score.isPassed() {
        fmt.Println("Pass!")
    } else {
        fmt.Println("Fail...")
    }
}

func main() {
    englishScore := englishScoreType(90)
    mathScore := mathScoreType(50)

    showResult(englishScore)
    showResult(mathScore)
}

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です