复杂对象 prblm stateme2 的常规闭包

发布于 2024-12-05 09:03:25 字数 1397 浏览 0 评论 0原文

我有一个 java 接口

    public interface IPerson {
      Person  addPerson(String name );
      Person  addPerson(String name1,String name2);
      Person  addPerson(String name,String[] details);
      Person  addPerson(String name,String name1,String[] details);
      Person  addPerson(String name,List<String> details);
    }

PersonImpl .java 是:

class PersonImpl implemets Iperson {
       ..
  // and interface methods implemtation

}

我的 person.java 看起来像

class Person {
    def firstName;
    def lastName;
}

我的 PersonTest.groovy 看起来像

def PersonImpl   person  = new PersonImpl();

person.addPerson("anish")
person.addPerson("anish","nath")
person.addPerson("john","smith")
person.addPerson("tim","yates")
def list=[];
list.add("abc")
list.add("qpr")
person.addPerson("anish",list)
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"])
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "String", "4"])

有什么办法为这个接口定义 DSL,以便我轻松调用 addOperation?

问题是IPerson 接口无法更改。

我怎样才能写 dsl 类似的东西

addPerson "anihs" "nath" //call to person.addPerson("anish","nath")
addPerson "tim" "kates" 

//simlary of other interface method any suggestion

I have an java interface

    public interface IPerson {
      Person  addPerson(String name );
      Person  addPerson(String name1,String name2);
      Person  addPerson(String name,String[] details);
      Person  addPerson(String name,String name1,String[] details);
      Person  addPerson(String name,List<String> details);
    }

With PersonImpl .java being:

class PersonImpl implemets Iperson {
       ..
  // and interface methods implemtation

}

and my person.java looks like

class Person {
    def firstName;
    def lastName;
}

And my PersonTest.groovy looks like

def PersonImpl   person  = new PersonImpl();

person.addPerson("anish")
person.addPerson("anish","nath")
person.addPerson("john","smith")
person.addPerson("tim","yates")
def list=[];
list.add("abc")
list.add("qpr")
person.addPerson("anish",list)
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"])
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "String", "4"])

Is there any way to define the DSL for this interface so that i easily called addOperation easily?

The problem is that the IPerson interface cannot be changed.

how can i write dsl something like

addPerson "anihs" "nath" //call to person.addPerson("anish","nath")
addPerson "tim" "kates" 

//simlary of other interface method any suggestion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-12-12 09:03:25

在 Groovy 中,像 addPerson "anihs" "nath" 这样的东西实际上是不允许的。如果您想要两个参数,则必须使用逗号。所以你能得到的最好的就是addPerson "anihs", "nath"。但这个方法调用只是悬而未决,上下文丢失了。当然,一个非常简单的版本是:

def PersonImpl person = new PersonImpl();
person.with {
  addPerson "anish"
  addPerson "anish","nath"
  addPerson "john","smith"
  addPerson "tim","yates"
  def list = ["abc", "qpr"]
  addPerson "anish",list
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"]
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "String", "4"]
}

尽管我不确定这对您来说是否足够。

In Groovy something like addPerson "anihs" "nath" is not really allowed. If you wanted two arguments you would have to use a comma. So the best you can get is addPerson "anihs", "nath". But this method calls is just hanging in the air, the context is missing. One quite easy version would be of course:

def PersonImpl person = new PersonImpl();
person.with {
  addPerson "anish"
  addPerson "anish","nath"
  addPerson "john","smith"
  addPerson "tim","yates"
  def list = ["abc", "qpr"]
  addPerson "anish",list
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"]
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "String", "4"]
}

though I am not sure this is enough for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文