[Training Video - 3] [Groovy in Detail] Non-static and Static functions, initializing log inside class
2024-10-15 13:54:04
log.info "starting" // we use class to create objects of a class
Planet p1 = new Planet()
Planet p2 = new Planet() //Planet.name = "Pluto" illegal
Planet.shape = "Circle" // static variable
Planet.log = log p1.name = "earth" // non static variable
p2.name = "jupiter" p1.printName() // non static has to be called with reference
Planet.revolve() // static can be called with class class Planet{
// variables and functions
def name // non static variable
def static shape // static variable
def static log public void printName(){ // non static function
log.info ("Name of planet is $name. Shape is $shape")
xyz() // non static can access static
} public static void revolve(){ // static function
//log.info (name) // error, static cannot access non static
xyz() // call one function from another function
log.info ("Planet revolving. Shape is $shape")
} public static void xyz(){
log.info "inside xyz"
}
}
Test Result:
Tue Oct 06 18:30:29 CST 2015:INFO:starting
Tue Oct 06 18:30:29 CST 2015:INFO:Name of planet is earth. Shape is Circle
Tue Oct 06 18:30:29 CST 2015:INFO:inside xyz
Tue Oct 06 18:30:29 CST 2015:INFO:inside xyz
Tue Oct 06 18:30:29 CST 2015:INFO:Planet revolving. Shape is Circle
Note :
Static cannot access non static
最新文章
- clr 元数据
- 猫哥网络编程系列:详解 BAT 面试题
- Sprint评分表
- 使用opencv设置图像的格式以及帧率
- Java学习笔记之:Java JDBC
- js混淆工具
- 李洪强iOS开发之【零基础学习iOS开发】【01-前言】02-准备
- 《Differential Equations with Boundary-Value Problems》-chaper2-一阶线性方程
- D - 粉碎叛乱F - 其他起义
- Django - 模型表单(创建、更新、删除)
- 【转】iOS开发路线简述
- [mysql]ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value 解决方法
- iOS下WebRTC音视频通话(三)-音视频通话
- vue相关操作命令
- Redis安装完后redis-cli无法使用(redis-cli: command not found)已使用
- gensim_主题提取
- Python-WSGI协议如何实现?
- 使用pidstat监控资源使用
- c++ 判断容器A是否是容器B的子集,如果是,返回true(includes)
- 【转】C#中continue、break和return用法