为什么我无法从另一个 Groovy 类实例化一个 Groovy 类?

2024-01-19

我有两节课。一.groovy:

class One {

  One() {}

  def someMethod(String hey) {
    println(hey)
  }
}

还有两个.groovy:

class Two {

  def one

  Two() {
    Class groovy = ((GroovyClassLoader) this.class.classLoader).parseClass("One.groovy")
    one = groovy.newInstance()
    one.someMethod("Yo!")
  }
}

我用这样的东西实例化二:

GroovyClassLoader gcl = new GroovyClassLoader();
Class cl = gcl.parseClass(new File("Two.groovy"));
Object instance = cl.newInstance();

但现在我明白了groovy.lang.MissingMethodException: No signature of method: script13561062248721121730020.someMethod() is applicable for argument types: (java.lang.String) values: [Yo!]

有任何想法吗?


似乎这是由于调用了 groovy 类加载器方法而发生的:一串 http://groovy.codehaus.org/gapi/groovy/lang/GroovyClassLoader.html#parseClass%28java.lang.String%29是解析文本格式的脚本。使用File一个人在这里工作:

class Two {

  def one

  Two() {
    Class groovy = ((GroovyClassLoader) this.class.classLoader).parseClass("One.groovy")
    assert groovy.superclass == Script // whoops, not what we wanted

    Class groovy2 = ((GroovyClassLoader) this.class.classLoader).parseClass(new File("One.groovy"))
    one = groovy2.newInstance()
    assert one.class == One // now we are talking :-)


    one.someMethod("Yo!") // prints fine

  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我无法从另一个 Groovy 类实例化一个 Groovy 类? 的相关文章

  • 如何在 Groovy 中设置最后一个字段

    我有一个在 groovy spock 中测试的 java 类 java 类有一个final 字段 private static final log Logger getLogger 我想测试一个方法是否使用此记录器 最好使用模拟 问题是这个
  • C# 中的类和模块有什么用

    有人可以解释一下类和模块之间的区别吗 你什么时候使用其中一种而不是另一种 我正在使用 C 更新 我的意思是相当于 VB 模块的 C 版本 这在很大程度上取决于您所指的 模块 Visual Basic 的模块 C 中没有真正等效的 VB Ne
  • Jenkins 管道中的动态变量与 Groovy 方法变量

    我在 Groovy 中有一个用于声明性管道的 Jenkinsfile 以及两个创建的 Jenkins 变量 其名称为 OCP TOKEN VALUE ONE 和 OCP TOKEN VALUE TWO 以及相应的值 当我尝试传递方法变量并在
  • 如何为带有继承的 C++ 类编写 C 包装器

    我只是想知道是否有一种方法可以为具有继承的 C 类创建 C 包装 API 考虑以下 class sampleClass1 public sampleClass public int get return this data 2 void s
  • 从模板类创建对象时出错

    我一直在尝试找到一种方法 从 C 中的多元正态分布中采样随机向量 同时具有均值向量和协方差矩阵 就像 Matlab 的那样mvnrnd功能有效 我找到了实现此功能的类的相关代码这一页 http lost found wandering bl
  • 在 Ruby 中跨多个类实例记忆数据的好方法是什么?

    考虑 生成数据的对象的许多实例 如果每次运行只生成一次该数据 那就太好了 class HighOfNPeriods lt Indicator def generate data indicator data DataStream new 0
  • C++类名冲突

    我现在正在做一个项目 需要整合两个子项目 项目A是用C 编写的 项目B是用C编写的 一个问题是 在项目B中 有一个名为vector它是由其作者创建的 在项目 A 中 std vector in STL用来 因为项目B以后可能会更新 所以我不
  • 带 getClassLoader 和不带 getClassLoader 的 getResourceAsStream 有什么区别?

    我想知道以下两者之间的区别 MyClass class getClassLoader getResourceAsStream path to my properties and MyClass class getResourceAsStre
  • AppDelegate 的变量用作全局变量不起作用

    我想使用我的 AppDelegate 来存储任何其他类都可以访问的对象 我已经像这样声明了这个 AppDelegate interface MyAppDelegate UIResponder
  • 使用 jQuery 更改 CSS 类属性

    有没有办法使用 jQuery 更改 CSS 类的属性 而不是元素属性 这是一个实际的例子 我有一个 div 类red red background red 我想转班级red背景属性 而不是具有类的元素red分配的背景 如果我用 jQuery
  • C++:获取器和设置器?

    我正在尝试编写一些代码来为以下数据的 ID 号 名字 姓氏 期中成绩和期末成绩创建 getter 和 setter 这些数据位于我正在编写的班级的文本文件中 10601 ANDRES HYUN 88 91 94 94 89 84 94 84
  • Swift:协议、结构、类

    我开始学习 Swift 语言 但在理解协议 结构和类方面遇到了困难 我来自 Android 方面的编程 所以我相信 Swift 协议基本上是 Java 接口 其中每一个的正确用例是什么 这些类比并不 完全 正确 但这就是我所理解的要点 是的
  • R 中使用 `UseMethod()` 与 `inherits()` 来确定对象的类

    如果我需要根据 R 对象的类以不同的方式处理它们 我可以使用if and else在单个函数内 foo lt function x if inherits x list Foo the list else if inherits x num
  • groovy中bigdecimal的默认比例

    groovy 中 BigDecimal 的默认小数位数是多少 和四舍五入 因此 当尝试进行计算时 def x 10 0 30 0 0 3333333333 def y 20 0 30 0 0 6666666667 基于此 我可以假设它使用比
  • 为什么基类必须有一个带有 0 个参数的构造函数?

    这不会编译 namespace Constructor0Args class Base public Base int x class Derived Base class Program static void Main string a
  • 无法从 C# WPF 中的另一个窗口调用方法

    好吧 假设我有两个窗户 在第一个中我有一个方法 public void Test Label Content works 在第二个方法中 我称此方法为 MainWindow mw new MainWindow mw Test 但什么也没发生
  • Delphi - 如果没有创建类,为什么这个函数可以工作?

    考虑这个类 unit Unit2 interface type TTeste class private texto string public function soma a b integer string end implementa
  • Groovy 元编程 - 将静态方法添加到 Object.metaClass

    我遇到了无法解决的 Groovy 元编程问题 将静态方法 foo 添加到类 FooBar 时 FooBar foo 按预期工作 FooBar metaClass static foo println hello FooBar foo 但是
  • 类方法作为 JavaScript 中的事件处理程序?

    JavaScript 中是否有最佳实践或通用方法将类成员作为事件处理程序 考虑以下简单示例
  • 自定义类的重写 bool() [重复]

    这个问题在这里已经有答案了 我想要的只是 bool myInstance 返回 False 并且 myInstance 在 if or and 等条件下计算为 False 我知道如何覆盖 gt 我试过这个 class test def bo

随机推荐