小编Cal*_*lid的帖子

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

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

从本质上讲,有没有办法实现这一点,而不会牺牲以下三个属性轮廓中的任何一个main(): …

groovy multiple-inheritance typechecking

4
推荐指数
1
解决办法
3743
查看次数