Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为 Parameter 的 Java 对象,我正在尝试使用 groovy 来模拟它。参数是具有1个抽象方法的抽象类。它还有一个名为 getName() 的非抽象方法。我试图在 Groovy 中模拟如下:
def p1 = [name:{"p1Name"}] as Parameter
但是我得到一个运行时错误,因为我没有实现抽象方法。如果我试图创建一个模拟,为什么我需要实现抽象方法?
谢谢,杰夫
通过使用地图进行模拟,您正在创建 Parameter 类型的实例,因此它必须实现 Parameter 类的任何抽象方法。
abstract class Parameter { abstract String getOtherName() String getName() { return "test" } } def p1 = [name:{"p1Name"}, getOtherName:{""}] as Parameter