为什么我的 Groovy println 在正确的语句后总是 return null?
Why does my Groovy println always return null after correct statement?
我是 groovy 的新手,我想知道为什么我在输入代码后收到空值。我有一个 App.groovy 和一个 Person.groovy,它们都是非常基本的,我认为我的错误是在自动 return 中关闭,但我不确定
package firstGradleGroovyBuild
class App {
static void main(def args) {
def myList = [1,2,"James","4"]
myList.each {
println("$it is of ${it.class}")
}
println()
Person person = new Person()
person.age = 13
person.address = "Home"
println(person)
Person p2 = new Person()
p2.firstName = "Joe"
p2.lastName = "Shmo"
p2.age = 23
p2.address = "Away"
println p2
Person p3 = new Person(firstName: "Smoky", lastName: "Robinson", age: 24, address: "Mountains")
println p3
}
}
这是我的应用程序,我只是想测试一些不同的东西。
首先,我想确保在不设置名字和姓氏的情况下,我会在他们的位置收到 null。
其次,我只是想测试正常的 属性 设置
第三,我想测试自动默认构造函数初始化道具。
在查看文档后,我也尝试用 tap{} 替换 p2(没有用),但我认为这是因为 tap 仅用作一种偏好,以节省在更新实例时一遍又一遍地键入前缀 p2 .
Person p2 = new Person().tap {
firstName = "Jo"
lastName = "Mo"
age = 23
address = "Away"
}
println p2
这是我的人class
package firstGradleGroovyBuild
class Person {
String firstName
String lastName
int age
def address
String toString(){
println("${firstName} ${lastName} of age ${age} lives at ${address}")
}
}
我的输出 几乎 符合预期:println 中的所有内容都是正确的。
1 is of class java.lang.Integer
2 is of class java.lang.Integer
James is of class java.lang.String
4 is of class java.lang.String
null null of age 13 lives at Home
null
Joe Shmo of age 23 lives at Away
null
Smoky Robinson of age 24 lives at Mountains
null
Process finished with exit code 0
但是我在每次 println 后得到一个 "null"。有人可以解释一下我错过了什么吗?
如您所见,println
returns null
所以在您的 toString
中,您正在打印字符串,然后返回 null
toString
不应进行任何打印。
将您的 class 更改为:
class Person {
String firstName
String lastName
int age
def address
String toString(){
"${firstName} ${lastName} of age ${age} lives at ${address}"
}
}
一切都会好的:-)
我是 groovy 的新手,我想知道为什么我在输入代码后收到空值。我有一个 App.groovy 和一个 Person.groovy,它们都是非常基本的,我认为我的错误是在自动 return 中关闭,但我不确定
package firstGradleGroovyBuild
class App {
static void main(def args) {
def myList = [1,2,"James","4"]
myList.each {
println("$it is of ${it.class}")
}
println()
Person person = new Person()
person.age = 13
person.address = "Home"
println(person)
Person p2 = new Person()
p2.firstName = "Joe"
p2.lastName = "Shmo"
p2.age = 23
p2.address = "Away"
println p2
Person p3 = new Person(firstName: "Smoky", lastName: "Robinson", age: 24, address: "Mountains")
println p3
}
}
这是我的应用程序,我只是想测试一些不同的东西。
首先,我想确保在不设置名字和姓氏的情况下,我会在他们的位置收到 null。
其次,我只是想测试正常的 属性 设置
第三,我想测试自动默认构造函数初始化道具。
在查看文档后,我也尝试用 tap{} 替换 p2(没有用),但我认为这是因为 tap 仅用作一种偏好,以节省在更新实例时一遍又一遍地键入前缀 p2 .
Person p2 = new Person().tap {
firstName = "Jo"
lastName = "Mo"
age = 23
address = "Away"
}
println p2
这是我的人class
package firstGradleGroovyBuild
class Person {
String firstName
String lastName
int age
def address
String toString(){
println("${firstName} ${lastName} of age ${age} lives at ${address}")
}
}
我的输出 几乎 符合预期:println 中的所有内容都是正确的。
1 is of class java.lang.Integer
2 is of class java.lang.Integer
James is of class java.lang.String
4 is of class java.lang.String
null null of age 13 lives at Home
null
Joe Shmo of age 23 lives at Away
null
Smoky Robinson of age 24 lives at Mountains
null
Process finished with exit code 0
但是我在每次 println 后得到一个 "null"。有人可以解释一下我错过了什么吗?
如您所见,println
returns null
所以在您的 toString
中,您正在打印字符串,然后返回 null
toString
不应进行任何打印。
将您的 class 更改为:
class Person {
String firstName
String lastName
int age
def address
String toString(){
"${firstName} ${lastName} of age ${age} lives at ${address}"
}
}
一切都会好的:-)