Jcat
宠辱不惊,闲看庭前花开花落~~
posts - 173,comments - 67,trackbacks - 0
Groovy把最常用的东西都设置成default了,经常可以省这省那的(比如括号、分号、public等);又加上闭包,初学的时候,有些代码,一眼望去,都分不清是方法、是类、还是闭包。
这里先简单总结一下常用的修饰符。

package jcat.bit.g

/*
修饰符有三类:
1. static
2. 作用域:public(默认), private, protected.
3. 类型:def(默认,动态), void(无类型,静态), 其它常规静态类型
*/
class Test {
    
static def a_static_def = 'a_static_def'
    
static a_static = 'a_static'
    def a_def 
= 'a_def'

    
static void main(strs) {
        println a_static_def
        println a_static

        println t_static_def()
        println t_static()


        
new Test().with {
            println a_def

            println t_public_def()
            println t_public()
            println t_def()
        }
    }

    
static def t_static_def() { // def = 可以返回任何类型,包括void
        return "t_static_def"
    }

    
static t_static() { // 有其它修饰符可以省略def(默认public + def
        return "t_static"
    }

    
public def t_public_def() {
        
return "t_public_def"
    }

    
public t_public() { // 默认def
        return "t_public"
    }

    def t_def() { 
// 至少要有一个修饰符,所以这个def不能省略; 且默认public
        return "t_def"
    }

    
static void t_static_void() {
        
//return "t_static_void"    // cannot return an object from a method that returns "void"
    }
}
posted on 2008-11-10 12:30 Jcat 阅读(1424) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: