码迷,mamicode.com
首页 > 其他好文 > 详细

[Training Video - 3] [Groovy in Detail] Non-static functions and Static functions,initializing log inside class,Objects and object referances

时间:2015-10-06 18:05:30      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

Planet.log = log

log.info "starting"
// we use class to create  objects of a class
Planet p1 = new Planet()
Planet p2 = new Planet()
Planet p3 = new Planet()

//Planet.name = "Pluto"  illegal
 Planet.shape = "Circle" 

p1.name = "earth"
//p1.shape = "circle"

p2.name = "jupiter"
//p2.shape = "circle"

p3.name = "mars"
//p3.shape = "circle"

log.info p1.name+"  "+p1.shape
log.info p2.name+"  "+p2.shape
log.info p3.name+"  "+p3.shape

p1 = p2
log.info "*************************************"
log.info p1.name+"  "+p1.shape
log.info p2.name+"  "+p2.shape
log.info p3.name+"  "+p3.shape

p1.name = "pluto"
log.info "*************************************"
log.info p1.name+"  "+p1.shape    //pluto
log.info p2.name+"  "+p2.shape    //pluto
log.info p3.name+"  "+p3.shape

p1.printName()   // non static has to be called with reference

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)
		log.info ("Name of planet is $name")
	}

	public static void revolve(){  // static function
		log.info ("Planet revolving")
	}
}

Test Result:

Tue Oct 06 17:46:18 CST 2015:INFO:starting
Tue Oct 06 17:46:18 CST 2015:INFO:earth  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:jupiter  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:mars  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:*************************************
Tue Oct 06 17:46:18 CST 2015:INFO:jupiter  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:jupiter  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:mars  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:*************************************
Tue Oct 06 17:46:18 CST 2015:INFO:pluto  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:pluto  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:mars  Circle
Tue Oct 06 17:46:18 CST 2015:INFO:Name of planet is pluto
Tue Oct 06 17:46:18 CST 2015:INFO:Name of planet is pluto

 

[Training Video - 3] [Groovy in Detail] Non-static functions and Static functions,initializing log inside class,Objects and object referances

标签:

原文地址:http://www.cnblogs.com/MasterMonkInTemple/p/4857445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!