groovy关闭复杂对象prblm stateme2 [英] groovy closure for complex object prblm stateme2

查看:145
本文介绍了groovy关闭复杂对象prblm stateme2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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实现Iperson {
..
//和接口方法实现


和我的person.java看起来像

  class Person {
def first 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,列表)
person.addPerson(nath, 11,[.docsDevNmAccessStatus.1,Integer,4])
person.addPerson(nath,11,[。.docsDevNmAccessStatus.1,String, 4])

是否有任何方法可以为此接口定义DSL,以便轻松调用addOperation容易吗?



问题在于 IPerson 界面无法更改。



我如何写dsl这样的东西:

  addPersonanihsnath//给人打电话。 addPerson(anish,nath)
addPersontimkates

//其他接口方法的模拟任何建议


解决方案

在Groovy中,像 addPersonanihsnath不是真的允许。如果你想要两个参数,你将不得不使用逗号。所以你可以得到最好的结果是 addPersonanihs,nath。但是这种方法呼叫只是悬在空中,环境缺失。一个非常简单的版本当然是:

  def PersonImpl person = new PersonImpl(); 
person.with {
addPersonanish
addPersonanish,nath
addPersonjohn,smith
addPersontim, yates
def list = [abc,qpr]
addPersonanish,list
addPersonnath,11,[.docsDevNmAccessStatus.1,整数,4]
addPersonnath,11,[.docsDevNmAccessStatus.1,String,4]
}

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

解决方案

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.

这篇关于groovy关闭复杂对象prblm stateme2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆