Last active
August 29, 2015 14:11
-
-
Save JustinSDK/4762a1d7cdb171c9d25e to your computer and use it in GitHub Desktop.
Haskell 次型態多型的模擬
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
更多 Haskell 的 OOP 可參考〈OOP vs type classes〉