groovy - 使用 trait 从外部类调用嵌套静态类内部的方法
问题描述
我在两个不同的 A、B 类中有一个静态类“Inner”和一个嵌套静态类“Deeper”。'Inner' 类实现了具有称为 ping() 的方法的特征 C。我想从 ping() 方法中执行方法 hello()(属于 Deeper),每次我都会根据调用该特征的类获得“Hello A”或“Hello B”。这就是我写的(我正在使用katalon-studio):
public class A
{
static class Inner implements C{
static class Deeper{
static void hello(){ println 'Hello A'}
}
}
}
public class B
{
static class Inner implements C{
static class Deeper{
static void hello(){ println 'Hello B'}
}
}
}
public static trait C {
static void ping() {
this.Deeper.hello()
}
}
A.Inner.ping()
B.Inner.ping()
我收到以下错误:
2018 年 8 月 17 日下午 4:46:57 - [错误] - 测试用例/V2/常规/草稿失败,因为没有为测试用例定义变量“更深”。
解决方案
找不到这个问题的答案。然而,使用非静态类,这可以实现如下:
public class A{
class Inner implements C{
A.Inner.Deeper d = new A.Inner.Deeper()
class Deeper {
void hello(){
println 'Hello A'
}
}
}
}
public class B{
class Inner implements C{
B.Inner.Deeper d = new B.Inner.Deeper()
class Deeper{
void hello(){
println 'Hello B'
}
}
}
}
public trait C{
public void ping(){
this.d.hello()
}
}
new A.Inner().ping()
new B.Inner().ping()
推荐阅读
- macos - 原因:macos应用找不到图片
- javascript - 如何在 `vue2-google-maps` 中使用`new google.maps.Marker()`
- ios - Swift更改UITabBarItem图像并动态着色
- javascript - 如何将来自 HTML 中动态制作的文本字段的输入存储/传递给 Django?
- php - 注意:未定义的偏移量:不同的行
- python-3.x - 如何计算字典中值之间的差异
- python - django makemigrations 中的操作错误 1050 错误“表已存在”
- python - 从 是什么意思。进口?
- javascript - 更改坐标时,不会创建航点
- python - TensorFlow 分布式训练中的数据准备和加载