Groovy def可选类型

2019-04-27 17:22|来源: 网路

Groovy是一个“可选”类型的语言,当理解语言的基本原理时,这种区别是一个重要的语言。与Java相比,Java是一种“强”类型的语言,由此编译器知道每个变量的所有类型,并且可以在编译时理解和尊重合同。这意味着方法调用能够在编译时确定。

当在Groovy中编写代码时,开发人员可以灵活地提供类型或不是类型。这可以提供一些简单的实现,并且当正确利用时,可以以强大和动态的方式为您的应用程序提供服务。

在Groovy中,可选的键入是通过'def'关键字完成的。下面是一个使用def方法的例子 -

class Example { 
   static void main(String[] args) { 
      // Example of an Integer using def 
      def a = 100; 
      println(a); 
		
      // Example of an float using def 
      def b = 100.10; 
      println(b); 
		
      // Example of an Double using def 
      def c = 100.101; 
      println(c);
		
      // Example of an String using def 
      def d = "HelloWorld"; 
      println(d); 
   } 
}

从上面的程序,我们可以看到,我们没有声明单个变量为Integer,float,double或string,即使它们包含这些类型的值。

当我们运行上面的程序,我们将得到以下结果 -

100 
100.10 
100.101
HelloWorld

可选的打字在开发期间可以是一个强大的实用程序,但是当代码变得太大和复杂时,可能导致在后期开发阶段的可维护性问题。

要了解如何使用Groovy中的可选输入,而不让代码库陷入无法维护的混乱,最好在应用程序中采用“鸭式输入”的理念。

如果我们使用鸭式重写上面的代码,它将看起来像下面给出的。变量名称的名称比它们代表的类型更多,这使得代码更容易理解。

class Example { 
   static void main(String[] args) { 
      // Example of an Integer using def 
      def aint = 100; 
      println(aint); 
		
      // Example of an float using def 
      def bfloat = 100.10; 
      println(bfloat); 
		
      // Example of an Double using def 
      def cDouble = 100.101; 
      println(cDouble);
		
      // Example of an String using def 
      def dString = "HelloWorld"; 
      println(dString); 
   } 
}

相关问答

更多
  • 它是基本脚本的语法糖。 省略“def”关键字将变量放在当前脚本的绑定中,groovy会像全局作用域变量一样处理它(主要是) x = 1 assert x == 1 assert this.binding.getVariable("x") == 1 使用def关键字不会将变量放在脚本绑定中: def y = 2 assert y == 2 try { this.binding.getVariable("y") } catch (groovy.lang.MissingPropertyExcept ...
  • 由于方便(特别是如果来自Java),很容易陷入到处使用def的陷阱, 但正如你所看到的,如果你知道什么类型的东西,最好是输入它,特别是在公共方法上。 好处包括; 自我记录,ide提示,理智...... It's easy to fall into the trap of using def everywhere, due to convenience (especially if coming from Java) But as you have seen, if you know the type of ...
  • 哇,本来希望快速回答这个问题! 好吧,找到了我自己的答案:除了以外你不能有多个例外。 请在此处查看功能请求 。 相反,他们建议创建一个过滤器。 看到这里 。 Wow, would have expected a quick answer on this! Well, found my own answer: you can't have more than one exception with except. See the feature request here. Instead, they recom ...
  • 关于什么 Ash.join().split("").tail() What about Ash.join().split("").tail()
  • 排序 方法在List类的父级上定义,即Collection 。 更新 正如您在您的评论中指出的那样,Groovy JDK文档遗憾地没有显示类层次结构。 通过查看(Oracle)API文档,您可以找出特定类型的父母/孩子。 例如,在本页的顶部,您可以看到List的超级接口是Collection和Iterable,而List接口本身由AbstractList,AbstractSequentialList,ArrayList等实现 The sort methods are defined on the paren ...
  • 用户daggett ist正确, final不会在Groovy中做出局部变量final。 关键字只对班级成员有影响。 这是一个小例子: package de.scrum_master.stackoverflow import spock.lang.Specification class MyTest extends Specification { def "Final local variables can be changed"() { when: final def a = 3 ...
  • 有问题的代码是: s='s=\\\';s[0..1]+s[3]+s[0..1]+s[2]*6+s[3..-1]*2';s[0..1]+s[3]+s[0..1]+s[2]*6+s[3..-1]*2 因此,左边的语句定义了一个字符串,然后右边的语句将字符串的不同位加在一起: 如果我们添加以下内容以打印出它正在做的事情: println s[0..1] println s[3] println s[0..1] println s[2] * 6 println s[3..-1] * 2 我们得到输出: s= ' ...
  • 你是对的 - 一个范围内的负数基本上是指列表的结尾 ,而不是开头。 -x相当于who.length()-x 。 您正在处理的内容在Python中称为切片 。 (我提到了这个术语,因为搜索像“groovy slices”这样的东西可能会帮助你找到更多的信息,虽然我不知道它们是否真的被称为“切片”参考Groovy。)你可以找到更多关于这个特定信息的信息。语法功能在这里 。 至于其他资源,我发现Groovy in Action这本书非常方便学习Groovy。 You're right -- a negative ...
  • 以下做你想要的(我认为) Files.newDirectoryStream(dir, { f -> f.fileName ==~ /.+\.txt/ }).withCloseable { stream -> stream.each { println it } } 如你所说,它不会递归目录 The following does what you want (I think) Files.newDirectoryStream(dir, { f -> f.fileName == ...
  • 它不会起作用。 它将此理解为对foo对象的getAt方法调用: foo["a", "b"] 然后逗号毫无意义。 你可以使用varargs: def foo(Map map, Object... args) { "$map $args" } a = foo "a", "b", bar: {true} println a // prints [bar:script_from_command_line$_run_closure1@1f3f7e0] [a, b] 或者反转参数顺序: def foo(Map ma ...