groovy動態語言

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

//動態語言測試

/**
 * @TypeChecked  檢測語法
 * respondsTo 檢測是否實現了某個方法、
 * hasProperty 檢測是否含有某個屬性
 * @CompileStatic 靜態加速運行速度
 */

class Lance {

    def person
    def dream() {
        println 'lance'
    }
}

/**
 * 檢測語法  @TypeChecked 學習
 */
@TypeChecked
class Perso {
    def perso() {
        println 'perso'
    }
}

/**
 *  respondsTo  學習
 * @param person
 * @return
 */
def fun(person) {
    /**
     * respondsTo 檢測是否實現了這個方法
     */
    if (person.respondsTo('perso')) {
        person.perso()
    }
    if (person.respondsTo('dream')) {
        person.dream()
    }
}
/**
 * 檢測是否有person這個屬性
 * hasProperty('person')
 */
println new Lance().hasProperty('person')

fun(new Perso())
fun(new Lance())
fun('')

@CompileStatic
class Test1 {
    static void main(args) {
        def start = System.nanoTime()
        for (i in 0..10000000) {

        }
        def end = System.nanoTime()
        println((end - start) / 1.0e9)
    }
}

發佈了57 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章