Groovy:有没有办法在使用类型检查的同时实现多重继承?

标签 groovy multiple-inheritance typechecking

@groovy.transform.TypeChecked
abstract class Entity {
    ...
    double getMass() {
        ...
    }
    ...
}

@groovy.transform.TypeChecked
abstract class Location {
    ...
    Entity[] getContent() {
        ...
    }
    ...
}

@groovy.transform.TypeChecked
abstract class Container {...}  //inherits, somehow, from both Location and Entity

@groovy.transform.TypeChecked
class Main {
    void main() {
        double x
        Container c = new Chest() //Chest extends Container
        Entity e = c
        x = e.mass
        Location l = c
        x = l.content  //Programmer error, should throw compile-time error
    }
}

本质上,有没有一种方法可以在不牺牲 main() 中的三个属性概述的情况下实现这一点? :
  • 直接访问字段,甚至是虚拟字段
  • 分配给两个父类(super class)
  • 类型检查(编译时)
  • 最佳答案

    我不认为你可以在类里面做到这一点。也许你想要 traits (讨论中 更新: available in Groovy 2.3 并且已经摇摆不定!)或者,对于纯动态解决方案,@Mixin ,你会用一个好的测试套件来支持它。

    我的猜测:@Delegate是你最好的 friend ,但就目前而言,你只能存储 Chest Container 中的对象类型变量。所以你需要一些接口(interface)。

    即使父类(super class)不在你的控制之下,你也可以使用 groovy as运算符使其实现接口(interface)。

    首先,我重写了您的类(class)以删除 abstract并添加接口(interface):

    import groovy.transform.TypeChecked as TC
    
    interface HasMass { double mass }
    interface HasContent { Entity[] getContent() }
    
    @TC class Entity implements HasMass { double mass }
    
    @TC class Location {
        Entity[] getContent() {
            [new Entity(mass: 10.0), new Entity(mass: 20.0)] as Entity[]
        }
    }
    

    注意我没有添加 HasContentLocation , 显示 as 的用法.

    二、来了ContainerChest . @Delegate添加并自动继承委托(delegate)的接口(interface):
    @TC 
    abstract class Container {
      @Delegate Location location = new Location()
      @Delegate Entity entity = new Entity()
    }
    
    
    @TC class Chest extends Container { }
    

    最后,只要您坚持接口(interface),它就可以进行类型检查:
    @TC class Mult {
        static main(args) {
            def x // use 'def' for flow-typing
            Container c = new Chest() //Chest extends Container
            HasMass e = c
            x = e.mass
            def l = c as HasContent
    
            x = l.content  //Programmer error, should throw compile-time error
    
            assert c.content.collect { Entity it -> it.mass } == [10.0, 20.0]
        }
    }
    

    关于Groovy:有没有办法在使用类型检查的同时实现多重继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448714/

    相关文章:

    groovy - 当命令有空格时,groovy 进程执行出现问题

    java - 用 BufferedInputStream 包装 PipedInputStream

    php - CodeIgniter 扩展多个 Controller ?

    bash - 密码不应是用户名脚本的一部分

    java - 在 groovy 中获取 "java.sql.SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER"

    c++ - 类继承 C++

    python - 用python多重继承类实现的好习惯?

    Java:Instanceof 和泛型

    python-3.x - 为什么 type(mock.MagicMock()) == mock.MagicMock 返回 False?

    spring - 从过滤器获取方法