- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试调试一些使用mixins的代码,并且能够将问题减少到此示例。我有一个通过mixin接收方法的父类,以及一个从父类继承的子类。如果我尝试在子类的实例上替换方法,则该方法可以正常工作,除非要替换的方法是在父类的实例上被调用之前被替换的。如果它已被调用,那么我将无法替换
所以这段代码:
class M {
protected foo() { println 'foo' }
}
@Mixin(M) class A {
def bar() { foo() }
}
class B extends A {}
def b = new B()
def a = new A()
a.bar() //<-- comment out this line and see the difference
b.metaClass.foo = {println 'winning'}
b.bar()
将产生:
foo
foo
winning
最佳答案
metaClass是在方法调用上查看的,mixin有其自己的处理程序。
两者都是延迟加载和静态加载,如果不调用方法,则不会发生静态延迟加载。
Mixins优先于metaClass覆盖,这就是为什么在初始化A时显示foo而不赢的原因。
为了在每个类上解析元,都需要在对象上定义一个元,您需要Object.class.metaClass(即B.metaClass)。
有趣的是,这产生了:
groovy.lang.MissingMethodException: No signature of method: B.foo() is applicable for argument types: () values: []
Possible solutions: foo(), foo(java.lang.Object), bar(), any(), use([Ljava.lang.Object;), find(groovy.lang.Closure)
class B extends A {
def foo() { println 'not winning' }
}
@Mixin(M)
class B extends A {
}
a.bar() //<-- comment out this line and see the difference
B.metaClass.foo = {println 'class winning'}
b.metaClass.foo = {println 'object winning'}
b.bar()
foo
class winning
class M {
protected foo() { println 'foo' }
}
@Mixin(M) class A {
def bar() { foo() }
}
class B extends A {
def bar() { foo() }
}
class C extends B {
def foo() { println 'wat' }
}
@Mixin(M)
class D extends C { }
def b = new B()
def a = new A()
def c = new C()
def d = new D()
a.bar() //<-- comment out this line and see the difference
b.metaClass.foo = {println 'winning'}
b.bar()
c.metaClass.foo = {println 'losing'}
c.bar()
d.metaClass.foo = {println 'draw'}
d.bar()
foo
winning
wat
wat
关于inheritance - Groovy在这里做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525376/
我有一些库脚本:lib1.groovy: def a(){ } lib2.groovy: def b(){ } lib3.groovy: def c(){ } 并想在其他脚本中使用它们:配置文件: a
我有下面的 Groovy 脚本,我需要将它放在集中式 Groovy 库中,然后从 Ready API 项目中的任何脚本访问 Groovy 中提到的类 路径 : D:\GroovyLib\com\Lin
看完后this link ,我想尝试Groovy++,但我有一个担心; Groovy 的所有语法在 Groovy++ 中都有效吗? 例如,我可以在 Groovy 中执行此操作: def list =
我在 Spring-boot 应用程序中混合了 Groovy 和 Java。休息 Controller 和数据访问是用 Groovy 编写的。配置主要使用Java。 根据 logback 文档,如果类
我已阅读how to simply import a groovy file in another groovy script 我想在一个 groovy 文件中定义常用函数,并从其他 groovy 文
你知道,我也知道,只要只有一个是公共(public)的,就可以用 Java 实现。但是,在 Groovy 中可以这样做吗?如果是的话,在什么条件下? 最佳答案 Java 和 Groovy 之间在可以放
~/groovy % tree . ├── lib │ ├── GTemplate.class │ └── GTemplate.groovy └── Simple.groovy class
给定一个具有属性和构造函数的对象,我希望将构造函数参数复制到属性中,然后在构造函数中做一些额外的工作。 import groovy.transform.TupleConstructor @TupleC
我会提前道歉,我是 groovy 的新手。我的问题是我有 3 个执行不同功能的 groovy 脚本,我需要从我的主 groovy 脚本中调用它们,使用脚本 1 的输出作为脚本 2 的输入和脚本 2 的
我想在静态闭包中存储一些属性,然后在方法调用期间访问它们: class Person { static someMap = { key1: "value1", key2: "value2" } }
Groovy 是否有安全范围运算符? 例如,如果我有, [1,2,3][0..10] Groovy 会抛出一个 java.lang.IndexOutOfBoundsException: 有没有索引安全
在 Groovy 中使用 Maps/JsonBuilder 处理一些翻译/映射功能。 是否有可能(无需在 map 文字创建之外创建额外的代码).. 有条件地包含/排除某些键/值对?一些事情沿着以下路线
不知道我是否正确询问,但是我有类似以下内容: def x = 1 if (x == 1) { def answer = "yes" } println answer 我收到错误
我不明白 groovy 打字是如何工作的。在 wikipedia据说它具有很强的类型,但我可以在解释器上完美地做到这一点: 1 + '1' ==> 11 所以也许我很困惑,我不明白弱类型是什么,但我想
我对函数式编程概念非常陌生,正在观看 Neil Ford 在 youtube 中的演讲。 .在那里他谈到了一个计数器来演示一段代码而不使用全局状态(在 20:04)。来自 Java 世界,我很难理解这
我有两个问题。 我执行以下代码来查找 $ 的 ASCII 值: def a = "\$" def b = (int)a println b //prints 36 好吧,我对答案很满意。但是当我尝试像
只是想知道 时髦 像这样与默认值进行值匹配的方法? if(params.max != 10 && params.max != 20 && params.max != 30){ params.m
我最近正在读《行动中的格鲁夫》。在第7章中,它介绍了*。运算符(operator) 。当我运行有关此运算符的代码时,我会遇到一些错误。 class Invoice {
是否有易于阅读的方法或一些聪明的方法来制作 combination Groovy 中的元素?我知道 Iterable#combinations或 GroovyCollections#combinati
最近我下载了 Groovy-2.3.6 并尝试在 Linux 系统上安装它。我按照 http://groovy-lang.org/install.html 的说明进行操作.我设置了我的 GROOVY_
我是一名优秀的程序员,十分优秀!