Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save JustinSDK/4762a1d7cdb171c9d25e to your computer and use it in GitHub Desktop.

Select an option

Save JustinSDK/4762a1d7cdb171c9d25e to your computer and use it in GitHub Desktop.
Haskell 次型態多型的模擬
data Customer = Customer String Int deriving Show
data Vip = Vip Customer Float deriving Show
class CustBehavior c where
custAction :: Show c => c -> String
class CustBehavior c => VipBehavior c where
vipAction :: Show c => c -> String
instance CustBehavior Customer where
custAction c = show c ++ " cust"
instance CustBehavior Vip where
custAction c = show c ++ " custOfvip"
instance VipBehavior Vip where
vipAction c = show c ++ " vip"
doCustAction c = custAction c
doVipAction v = vipAction v
@JustinSDK
Copy link
Author

更多 Haskell 的 OOP 可參考〈OOP vs type classes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment