class
HelloWorld {
static
void
main(args) {
def
myString =
new
String(
"test String"
)
def
myObject =
new
Object()
int
myInt =
8
myObject =
3
println
"myString="
+ myString
println
"myObject="
+ myObject
println
"myInt="
+ myInt
}
}
|
class
HelloWorld {
def
void
test
(){
println
"this is test func"
}
def
String getString(String input){
println
"input="
+ input
return
"hello,"
+ input
}
static
void
main(args) {
def
hw =
new
HelloWorld()
hw.
test
()
println
hw.getString(
"Liuyou"
)
}
}
|
class
HelloWorld {
static
void
main(args) {
//ArrayList
可以动态增加大小
def
arrayList =
new
ArrayList()
arrayList[
0
] =
"dog"
arrayList[
1
] =
"cat"
arrayList[
2
] =
"bird"
for
(l
in
arrayList){
println
l +
" "
}
//list
,不可以动态增加大小
def
list = [
"dog"
,
"cat"
,
"bird"
]
for
(l
in
list){
println
l +
" "
}
//list array
def
lists = [[
"liuyou"
,
"22"
,
"M"
],[
"liudehua"
,
"33"
,
"M"
]]
for
(l
in
lists){
println
l[
0
] +
"-"
+ l[
1
] +
"-"
+ l[
2
]
}
for
(l
in
lists){
for
(e
in
l){
println
e
}
}
}
}
|
class
HelloWorld {
static
void
main(args) {
//
显示定义
map
def
map =
new
HashMap()
map.put(
"ID"
,
12345
)
map.put(
"name"
,
"Liuyou"
)
println
map.get(
"ID"
) +
"/"
+ map.get(
"name"
) +
"/"
+ map.get(
"email"
)
//
隐示定义
map
println
map.get(
"ID"
) +
"/"
+ map.get(
"name"
) +
"/"
+ map.get(
"email"
)
}
}
|
class
HelloWorld {
static
void
main(args) {
def
s =
"1234"
if
(s ==
"1234"
)
println
"yes,it is 1234"
def
n =
1234
if
(n ==
123
)
println
"yes, n is 123"
else
if
(n ==
12
)
println
"yes, n is 12"
else
if
(n ==
1234
)
println
"yes, n is 1234"
else
println
"yes, n is null"
}
}
|
class
HelloWorld {
static
void
main(args) {
def
s =
"1234"
switch
(s){
case
"1"
:
println
"1"
;
break
;
case
"2"
:
println
"2"
;
break
;
case
"1234"
:
println
"1234"
break
;
default
:
println
"default"
break
;
}
}
}
|
class
HelloWorld {
static
void
main(args) {
int
n =
10
while
(n){
println
"n="
+ n
n --
}
}
}
|
class
HelloWorld {
static
void
main(args) {
def
n = [
10
,
20
,
30
]
for
(e
in
n){
println
"e="
+ e
}
}
}
|
class
HelloWorld {
def
void
testException(){
try
{
def
n =
0
;
def
m =
2
;
def
l = m/n
}
catch
(Exception e){
println
e.toString()
}
}
def
void
testThrow(){
throw
new
java.lang.ArithmeticException()
}
static
void
main(args) {
def
hw =
new
HelloWorld()
//
除法零异常
hw.testException()
//
主动抛出异常
try
{
hw.testThrow()
}
catch
(ArithmeticException e){
println
e.toString()
}
}
}
|