• [Training Video


    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

  • 相关阅读:
    Linux防火墙管理(iptables)以及开放端口配置
    CSS 样式引入方式、常用选择器以及优先级权重的计算
    初识外边距合并-margin collapsing
    纯CSS实现自适应正方形
    常用正则
    vue 学习记录
    VScode 之快速创建vue模板
    vscode之常用插件
    工具函数
    Axios之配置参数
  • 原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/4857445.html
Copyright © 2020-2023  润新知