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