zoukankan      html  css  js  c++  java
  • 【itclx面向对象一】tcl基础语法:过程、作用域、以及itcl面向编程回顾

    学习熟悉编程的最好方法就是动手,有点面向编程思维的话,直接练习就可以。直接看demo

    1、过程、作用域

    #全局变量:过程外定义的变量
    #局部变量: 过程内部定义的变量

    set a 100
    proc testa {} {puts $::a}
    testa

    #作用域 : 全局作用域> 过程作用域1>过程作用域2......
    puts "********作用域***********"
    set ga 100;#全局
    proc test {} {set a 100;puts "test level [info level]"} ;#局部作用域名
    proc test1 {} {set b 200;test;puts "test1 level [info level]"}
    proc test2 {} {set c 2000;test1;puts "test2 level [info level]"}

    #test2>test1>test 怎么样来区分层次? [info level]
    puts "global: level [info level]"
    test2
    puts "********作用域***********"


    #1. 过程内部访问 全局变量/上层变量 的方法 1. global 2. upvar 3. uplevel 4. ::全局作用域名

    puts "global 访问全局变量"
    #***********global************
    #目标:在过程中改变全局变量的值。
    set g_a 100;#过程外定义全局变量a
    puts "g_a: $g_a"
    proc GetGlobal {} {;# 左括号一定要位于最好,以连接下一行
    global g_a
    set g_a 200 ;#在过程内被改变
    }
    GetGlobal
    puts "g_a:$g_a" ;# 经过检查确实被改变

    puts "global 访问全局变量 结束 "
    #*****************************


    puts "全局变量:: 开始"

    set g_ab 200
    proc setab {} {
    set ::g_ab 1000
    }
    setab
    puts "g_ab: $g_ab"
    puts "全局变量:: 结束 "

    puts "upvar 访问全局变量/上层变量/本层变量"
    set g_b 1000
    proc Getupvar {} {
    upvar g_b local_b
    puts "local_b: $local_b"
    set local_b 2000
    }
    puts "$g_b:$g_b"


    proc level1 {} {
    set b 200
    puts "b in level 1:$b"
    level2
    puts "b in level 1:$b"
    }
    proc level2 {} {
    upvar 1 b local_b
    set local_b 800
    }
    puts "upvar 访问全局变量/上层变量/本层变量/结束 "


    puts "uplevel改变全局变量/上级变量/本层变量 开始"

    set g_level0 100
    proc l1 {} {uplevel #0 {set g_level0 5000}}
    l1;#执行
    puts "after l1 g_level0: $g_level0"

    puts "uplevel改变全局变量/上级变量/本层变量 结束 "

    2、命名空间

    #命名空间
    #作用:防止同名变量冲突,对变量和过程 划分区域, 针对多个文件引用。

    #1. 基本概念
    set bb 2000;#全局变量

    namespace eval no1 {
    variable name 20
    proc testfun {} {puts "i am in no1"}
    }
    namespace eval no2 {
    variable name 30
    proc testfun {} {puts "i am in no2"}
    }
    puts "$no1::name"
    puts "$no2::name"
    no1::testfun
    no2::testfun

    puts "****************adv app*******"
    namespace eval test {
    namespace export get_var get_global
    variable aa 20
    variable bb 4000
    proc get_var {} {variable aa; set aa 200;return $aa}
    proc get_global {} {puts "global bb:$::bb"}
    }
    puts $test::aa
    puts "aa: [test::get_var]"
    test::get_global
    #2.导入和导出 省略命名空间
    puts "导入和导出,"
    namespace import test::get_var
    puts [get_var]
    namespace import test::get_global
    get_global
    #>>namespace import test::*

    #3.命名空间嵌套
    namespace eval t1 {
    namespace eval t2 {
    variable mytest 2000
    }
    }
    puts $t1::t2::mytest

    3、itcl基础编程

    #类定义
    #itcl::class className {
    # inherit baseClass ?baseClass...?
    # constructor args ?init? body
    # destructor body
    # method name ?args? ?body?
    # proc name ?args? ?body?
    # variable varName ?init? ?config?
    # common varName ?init?
    # public command ?arg arg ...?
    # protected command ?arg arg ...?
    # private command ?arg arg ...?
    # set varName ?value?
    # array option ?arg arg ...?
    # }
    package require itcl
    namespace import itcl::*
    #构造函数/析构函数==》 我轻轻的 “来” 了,挥一挥衣袖不带 “走” 一片云彩
    itcl::class Father {
    variable name "NoOne"
    variable money 0
    common peoples 0
    proc getpeople {} {puts "we have $peoples people"}
    method getmoney {} {return "[$this info class] has $money"}
    constructor {{mymoney 500}} {set money $mymoney;puts "have money $mymoney";incr peoples}
    destructor {puts "i am $this,good bye"; incr peoples -1}
    }
    Father f1
    puts [f1 getmoney]
    f1 getpeople
    #继承
    itcl::class Sun {
    inherit Father
    }
    Sun s1
    puts [s1 getmoney]
    #多态
    class SunOther {
    inherit Father
    method getmoney {} {puts "i am SunOther,not Father!";return "[$this info class] has $money"}
    }
    SunOther s2
    puts [s2 getmoney]
    # common 类变量
    # proc 类函数,大家都可以调用
    s1 getpeople
    s2 getpeople
    f1 getpeople
    #public,private,protected 描述父子关系
    class Mother {
    public method pubfunc {} {puts "pubfunc"}
    private method prifunc {} {puts "prifunc"}
    protected method profunc {} {puts profunc}
    ;#内部调用
    method runpub {} {pubfunc}
    method runpro {} {profunc}
    method runpri {} {prifunc}
    }
    Mother m1
    m1 pubfunc;#外部调用 OK

    #内部调用
    m1 runpub
    m1 runpro
    m1 runpri
    # m1 prifunc
    # m1 profunc
    class Dt {
    inherit Mother
    #
    method runpub {} {pubfunc}
    method runpro {} {profunc}
    method runpri {} {prifunc}
    #
    }

    Dt dt
    dt pubfunc

    dt runpub
    dt runpro
    dt runpri
    #失败

    # dt prifunc
    # dt profunc

  • 相关阅读:
    大数据实践解析(上):聊一聊spark的文件组织方式
    npm 项目更换目录后无法启动
    Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
    Vue 爬坑之路(五)—— 组件进阶
    Vue 爬坑之路(四)—— 与 Vuex 的第一次接触
    Vue 爬坑之路(三)—— 使用 vue-router 跳转页面
    Vue 爬坑之路(二)—— 组件之间的数据传递
    Vue 爬坑之路(一)—— 使用 vue-cli 搭建项目
    同步类的基础AbstractQueuedSynchronizer(AQS)
    由于不当的执行顺序导致的死锁
  • 原文地址:https://www.cnblogs.com/nemolmt/p/6228721.html
Copyright © 2011-2022 走看看