V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
trihuan
V2EX  ›  编程

请教一个关于函数子类的问题

  •  
  •   trihuan · 2019-12-15 09:23:59 +08:00 · 4408 次点击
    这是一个创建于 1584 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如我们有 类 vehicle {速度: int, 价格: int}, boat{速度: int, 价格: int, 排水量:int}, 所以船类是交通工具类的子类

    现在有 f ( g: Vehicle → int) = ... g( new(Vehicle)), 意思是函数 f 以 另一个函数 g : Vehicle → int 为参数

    1. 请问我可以让 f 以( h: Boat → int) 为参数吗?可以举一个 f 的具体例子吗?

    2. 我知道 (Vehicle → int) 是 (Boat → int)的子类,但是我只知道这个是对的,但是理解不了很迷糊,可以解释一下吗?



    谢谢!
    5 条回复    2019-12-15 14:34:02 +08:00
    ipwx
        1
    ipwx  
       2019-12-15 09:48:07 +08:00 via Android
    首先,你这啥语言的。不是所有语言都有这种函数子类的概念。然后比如 c++,这里就得用模板函数,加个 enable if,就行了,反正不用搞这种函数子类的问题。
    trihuan
        2
    trihuan  
    OP
       2019-12-15 09:52:04 +08:00
    @ipwx 是 scala
    trihuan
        3
    trihuan  
    OP
       2019-12-15 09:52:14 +08:00
    @ipwx 考试题。。
    secondwtq
        4
    secondwtq  
       2019-12-15 13:22:19 +08:00 via iPhone
    搜 variance
    另外这个不叫”子类”,subtyping 和 inheritance 不一样
    imn1
        5
    imn1  
       2019-12-15 14:34:02 +08:00
    我觉得合理的做法是交通工具作为一个抽象类,作为接口
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:37 · PVG 23:37 · LAX 08:37 · JFK 11:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.