为什么 Groovy 将 java.lang.String.length() 替换为 size()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31433991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:37:09  来源:igfitidea点击:

Why does Groovy replace java.lang.String.length() with size()?

javastringgroovyjvm-languages

提问by qntm

Wikipedia's current articleabout the Groovy programming language explains that "Most valid Java files are also valid Groovy files" and gives the following examples, first of Java code:

维基百科当前关于 Groovy 编程语言的文章解释了“大多数有效的 Java 文件也是有效的 Groovy 文件”,并给出了以下示例,首先是 Java 代码:

for (String it : new String[] {"Rod", "Carlos", "Chris"})
    if (it.length() <= 4)
        System.out.println(it);

Then the same in Groovy:

然后在 Groovy 中也是如此:

["Rod", "Carlos", "Chris"].findAll{it.size() <= 4}.each{println it}

Notice in the first example that we used the perfectly ordinary Java method, java.lang.String.length(). In the second example this method has been mysteriously replaced with a call to a method called size(). I have verifiedthat the second example is valid Groovy code and has the correct behaviour.

请注意,在第一个示例中,我们使用了非常普通的 Java 方法java.lang.String.length()。在第二个示例中,此方法已神秘地替换为对名为 的方法的调用size()。我已经验证第二个示例是有效的 Groovy 代码并且具有正确的行为。

java.lang.Stringdoes not have a method called size(). Groovy does not subclass Stringfor its own purposes:

java.lang.String没有称为 的方法size()。Groovy 不会String出于自己的目的进行子类化:

String s = ""
Class c = s.getClass()
println c.getName() // "java.lang.String"

nor does it somehow add extra methods to the Stringobject:

它也不会以某种方式向String对象添加额外的方法:

// [...]
for (def method : c.getMethods()) {
    println method.getName()
}
// prints a whole bunch of method names, no "size"

and yet still this code somehow works:

然而这段代码仍然有效:

// [...]
println s.size() // "0"

I can't find any Groovy documentation to explain this.

我找不到任何 Groovy 文档来解释这一点。

  • Where does size()come from?
  • Why does it not appear on the object?
  • Why was it added?
  • What was wrong with length()and why is it not preferred?
  • What other extra methods have been added to java.lang.String?
  • What about other standard classes?
  • 哪里size()来的呢?
  • 为什么它不会出现在物体上?
  • 为什么添加?
  • 有什么问题,length()为什么不是首选?
  • 还添加了哪些其他额外方法java.lang.String
  • 其他标准班呢?

回答by doelleri

Groovy adds lots of methods to strings and all sorts of other classes. All of the convenience methods are part of why Groovy is great.

Groovy 为字符串和各种其他类添加了许多方法。所有便利的方法都是 Groovy 出色的部分原因。

java.lang.Stringimplements java.lang.CharSequence, and that's where it gets all (most of) the magic from. size(), for example. Groovy adds a size()method to most objects that can have something you'd consider a size so that you can use a consistent method across the board. length()is still entirely valid, Groovy doesn't remove this.

java.lang.Stringimplements java.lang.CharSequence,这就是它获得所有(大部分)魔法的地方。size(), 例如。Groovysize()为大多数对象添加了一种方法,这些对象可以具有您认为大小的内容,以便您可以全面使用一致的方法。length()仍然完全有效,Groovy 不会删除它。

To see some of the methods Groovy adds, check out the GDK, and particularly CharSequenceand Collection.

要查看 Groovy 添加的一些方法,请查看GDK,尤其是CharSequenceCollection

回答by Thiht

I suggest you read the doc of the Groovy class StringGroovyMethods, it gives simple explanations of how things work with Groovy.

我建议您阅读 Groovy 类的文档StringGroovyMethods,它对 Groovy 的工作方式进行了简单的解释。

Static methods are used with the first parameter being the destination class, e.g.. public static String reverse(String self) provides a reverse() method for String.

静态方法与第一个参数是目标类一起使用,例如。public static String reverse(String self) 为String 提供了一个reverse() 方法。