没有合适的资源?快使用搜索试试~ 我知道了~
首页面对对象的TCL编程--ITCL [incr TCL]
资源详情
资源评论
资源推荐
0:Object-Oriented Programming
with [incr Tcl]
0:Building Mega-Widgets
with [incr Tk]
Michael J. McLennan
Bell Labs Innovations for Lucent Technologies
1247 S. Cedar Crest Blvd.
Allentown, PA 18104
mmclennan@lucent.com
Copyright © 1996 Lucent Technologies
ABSTRACT
Applications with short development cycles have the best chance for success in
today’s marketplace. Tcl/Tk provides an interactive development environment
for building Graphical User Interface (GUI) applications with incredible speed.
Tcl/Tk applications look like they were constructed with the Motif toolkit, but
they can be written in a fraction of the time. This is due, in part, to the high-
level programming interface that the Tcl language provides. It is also due to the
interpretive nature of the Tcl language; changes made to a Tcl/Tk application
can be seen immediately, without waiting for the usual compile/link/run cycle.
Developers can prototype new ideas, review them with customers, and deliver a
finished product within a span of several weeks. The finished product will run
on all of the major platforms: Unix, PC Windows, and Macintosh.
But the Tcl language was not designed to support large programming projects.
When Tcl/Tk scripts grow larger than a thousand lines, the code complexity can
be difficult to manage. [INCR TCL] extends the Tcl language to support object-
oriented programming. This allows developers to write high-level building
blocks that are more easily assembled into a finished application. The resulting
code has more encapsulation, and is easier to maintain and extend. [INCR TCL]
is patterned after C++, so for many developers, it is easy to learn.
This memo contains two chapters that will appear in a book published by
O’Reilly and Associates. It provides an overview of [INCR TCL], and shows
how it can be used to support Tcl/Tk applications. It also describes a special
library of base classes called [INCR TK], which can be used to build high-level
user interface components called “mega-widgets”.
3
Chapter 1Tcl/Tk Tools
In this Chapter:
• Objects and Classes
• Inheritance
• Namespaces
• Interactive
Development
• Autoloading
• Adding C code to
[INCR TCL] Classes
1
1:Object-Oriented
Programming with
[incr Tcl]
Tcl/Tk applications come together with astounding
speed. You can write a simple file browser in an
afternoon, or a card game like Solitaire within a
week. But as applications get larger, Tcl code
becomes more difficult to understand and maintain.
You get lost in the mass of procedures and global
variables that make up your program. It is hard to
create data structures, and even harder to make reus-
able libraries.
[INCR TCL] extends the Tcl language to support object-oriented programming.
It wasn’t created as an academic exercise, nor to be buzzword-compatible with
the latest trend. It was created to solve real problems, so that Tcl could be used
to build large applications.
[INCR TCL] is fully backward-compatible with normal Tcl, so it will run all of
your existing Tcl/Tk programs. It simply adds some extra commands which let
you create and manipulate objects.
It extends the Tcl language in the same way that C++ extends the base language
C. It borrows some familiar concepts from C++,
†
so many developers find it
easy to learn. But while it resembles C++, it is written to be consistent with the
Tcl language. This is reflected in its name, which you can pronounce as “incre-
ment tickle” or “inker tickle.” This is the Tcl way of saying “Tcl++”.
† Stanley B. Lippman, C++ Primer (2nd edition), Addison-Wesley, 1991; and Bjarne Stroustrup, The
Design and Evolution of C++, Addison-Wesley, 1994.
Tcl/Tk Tools
4
This chapter shows how [INCR TCL] can be used to solve common programming
problems. As an example, it shows how a tree data structure can be created and
used to build a file browser. Along the way, it illustrates many important
concepts of object-oriented programming, including encapsulation, inheritance,
and composition.
Objects and Classes
I won’t go on for pages about object-oriented programming. You have prob-
ably read about it in other contexts, and there are some really good texts
†
that
explain it well. But the basic idea is that you create objects as building blocks
for your application. If you are building a kitchen, for example, you might need
objects like toasters, blenders and can openers. If you are building a large
kitchen, you might have many different toasters, but they all have the same char-
acteristics. They all belong to the same class, in this case a class called
Toaster.
Each object has some data associated with it. A toaster might have a certain
heat setting and a crumb tray that collects the crumbs that fall off each time it
toasts bread. Each toaster has its own heat setting and its own crumb count, so
each Toaster object has its own variables to represent these things. In object
speak, these variables are called instance variables or data members. You can
use these instead of global variables to represent your data.
You tell an object to do something using special procedures called methods or
member functions. For example, a Toaster object might have a method called
toast that you use to toast bread, and another method called clean that you use
to clean out the crumb tray. Methods let you define a few strictly limited ways
to access the data in a class, which helps you prevent many errors.
Everything that you need to know about an object is described in its class defini-
tion. The class definition lists the instance variables that hold an object’s data
and the methods that are used to manipulate the object. It acts like a blueprint
for creating objects. Objects themselves are often called instances of the class
that they belong to.
Variables and Methods
Let’s see how objects work in a real-life example. Suppose you want to use the
Tk canvas widget to build a file browser. It might look something like the one
† For example: Grady Booch, Object-Oriented Design, Benjamin/Cummings, 1991; and Timothy
Budd, An Introduction to Object-Oriented Programming, Addison-Wesley, 1991.
Chapter 1: Object-Oriented Programming with [incr Tcl]
5
shown in Figure 1-1. Each entry would have an icon next to the file name to
indicate whether the file is an ordinary file, a directory, or an executable
program. Aligning each icon with its file name is ordinarily a lot of work, but
you can make your job much simpler if you create an object to represent each
icon and its associated file name. When you need to add an entry to the file
browser, you simply create a new object with an icon and a text string, and tell
it to draw itself on the canvas.
We will create a class VisualRep to characterize these objects. The class defini-
tion is contained in the file itcl/tree/visrep.itcl on the CD-ROM that
accompanies this book, and it appears in Example 1-1.
Figure 1-1 Using VisualRep objects to build a file browser.
Example 1-1 The class definition for VisualRep objects.
image create photo default -file default.gif
class VisualRep {
variable canvas
variable icon
variable title
constructor {cwin ival tval} {
set canvas $cwin
set icon $ival
set title $tval
}
destructor {
erase
}
method draw {x y} {
erase
$canvas create image $x $y -image $icon -anchor c -tags $this
set x1 [expr $x + [image width $icon]/2 + 4]
VisualRep
objects
剩余117页未读,继续阅读
corcoer2
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- ExcelVBA中的Range和Cells用法说明.pdf
- 基于单片机的电梯控制模型设计.doc
- 主成分分析和因子分析.pptx
- 共享笔记服务系统论文.doc
- 基于数据治理体系的数据中台实践分享.pptx
- 变压器的铭牌和额定值.pptx
- 计算机网络课程设计报告--用winsock设计Ping应用程序.doc
- 高电压技术课件:第03章 液体和固体介质的电气特性.pdf
- Oracle商务智能精华介绍.pptx
- 基于单片机的输液滴速控制系统设计文档.doc
- dw考试题 5套.pdf
- 学生档案管理系统详细设计说明书.doc
- 操作系统PPT课件.pptx
- 智慧路边停车管理系统方案.pptx
- 【企业内控系列】企业内部控制之人力资源管理控制(17页).doc
- 温度传感器分类与特点.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论2