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

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
时间: 2024-10-18 07:13:10

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

[Training Video - 3] [Groovy in Detail] Non-static and Static variables in groovy

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" /

[Training Video - 3] [Groovy in Detail] What is a groovy class ?

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

[Training Video - 4] [Groovy] Optional parameter in groovy

Employee.log=log Employee e1 = new Employee() log.info e1.add(1,2,3,4) // optional parameters in groovy log.info e1.add(2,3) log.info e1.add(2,3,10) class Employee { def static log public def add(a,b,c=3,d=10){ return a+b+c+d } } Run result: Tue Oct

[Training Video - 4] [Groovy] Constructors in groovy, this keyword

Bank.log = log Bank b1 = new Bank() b1.name = "BOA" b1.minbalance = 100 b1.city="London" Bank b2 = new Bank() b2.name = "HSBC" b2.minbalance = 100 b2.city="LA" Bank b3 = new Bank("A",100,"X") log

[Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Exception Handling in groovy

def x = new String[3] x[0] = "A" x[1] = "B" x[2] = "C" log.info"XXXXXX 1" try{ x[3] = "D" // def z=9/0 }catch(Exception e){ log.info "Some error "+e.getMessage() // Use e.getMessage() to print ex

static, const 和 static const 变量的初始化问题

const 常量的在超出其作用域的时候会被释放,但是 static 静态变量在其作用域之外并没有释放,只是不能访问. static 修饰的是静态变量,静态函数.对于类来说,静态成员和静态函数是属于整个类的,而不是属于对象.可以通过类名来访问,但是其作用域限制于包含它的文件中. static 变量在类内部声明,但是必须在类的外部进行定义和初始化. const 常量在类内部声明,但是定义只能在构造函数的初始化列表进行. class A { public: A(int a) : constNum(a)

Java中的内存处理机制和final、static、final static总结

Java中的内存处理机制和final.static.final static总结 装载自:http://blog.csdn.net/wqthaha/article/details/20923579 Java程序运行在JVM上,可以把JVM理解成Java程序和操作系统之间的桥梁,JVM实现了Java的平台无关性,由此可见JVM的重要性.所以在学习Java内存分配原理的时候一定要牢记这一切都是在JVM中进行的,JVM是内存分配原理的基础与前提.         一个完整的Java程序运行过程会涉及以

Java中主类中定义方法加static和不加static的区别

Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用,后者必须先实例化后用实例调用) 知识点:1.Getter and Setter 的应用 2.局部变量与成员变量(也可叫做全局变量) 3.Static关键字的用法 a.成员变量被static修饰后的所有类的共享属性 b.方法被static修饰之后,在本类内调用的类名省略问题;以及不用Static,即使在本类内也必须先实例化 4.This关键字的用法 this:是当前类的对象引用.简单的记,它就代表当前

C++中类中常规变量、const、static、static const(const static)成员变量的声明和初始化

C++类有几种类型的数据成员:普通类型.常量(const).静态(static).静态常量(static const).这里分别探讨以下他们在C++11之前和之后的初始化方式. c++11之前版本的初始化 在C++11之前常规的数据成员变量只能在构造函数里和初始化列表里进行初始化.const类型的成员变量只能在初始化列表里并且必须在这里进行初始化.static类型只能在类外进行初始化.static const类型除了整型数可以在类内初始化,其他的只能在类外初始化.代码如下: class A {