Reading Notes on NS2(2)

本文介绍了OTcl的基本概念,包括类的定义、构造与析构函数、成员变量与函数、继承及多继承等内容,并提供了丰富的示例代码。

[1] OTcl
OTcl称为Object Tcl,它是在Tcl的基础上做了一个面向对象的封装,是一种面向对象的脚本语言。
注意:
(1)OTcl的编程风格与C++有很大不同;

[2] 类定义
如何定义一个类?
例如:
% Class Fruits                ;#定义一个类
Fruits
% Fruits apple                ;#创建一个对象实例
apple  
% apple info class          ;#查看实例属于哪一个类
Fruits
% Fruits info instances   ;#查看类拥有的实例
apple

[3] 构造函数与析构函数
OTcl同样提供构造函数和析构函数,使类对象能够轻巧地被创建和撤销。(作用同C++)
注意:
(1)OTcl中,构造函数名和析构函数名不需要同类名相同;
(2)一般,通过调用init过程来实现类的构造函数,调用destroy过程完成类的析构;
(3)OTcl中,构造函数和析构函数不会自动调用父类的构造函数和析构函数,必须通过$self next命令显式调用父类的构造函数和析构函数;
例如:
Safety instproc init{} {    ;#构造函数(Safety为类名)
 $self next
 $self set count 0
}
Safety instproc destroy{} { ;#析构函数
 $self next
}

[4] 成员变量和成员函数
注意:
(1)OTcl中,成员变量并不需要事先定义,在成员函数需要使用它的时候,再在函数体内定义;
(2)定义成员变量使用关键字instvar;
(3)在所有的成员函数中要使用已经声明了的成员变量必须使用instvar来重新声明,否则变量只是当做函数内局部变量;
(4)成员函数所有的成员变量都是基于public的;
(5)定义成员函数使用关键字instproc;
(6)定义成员函数与定义一个普通的过程相同,只是在前面指定类名即可;(表明它是哪个类的成员函数)
(7)所有的成员函数也都是public的;
例如:
#定义类Fruits的一个init成员函数;
#变量$self表示对象本身(像C++中的this指针);
#next是指父类的同名方法(像C++中的覆盖);
% Fruits instproc init{args} {
   $self set instvar color 0    ;#初始化成员变量为0
   eval $self next $args
}
#
% Fruits instproc show_color{} {
   set color green
   puts "the color of fruit is $color"
}
% Fruits apple
% apple set color               ;#查看成员变量color的值
0
% apple show_color           ;#调用Fruits的show_color方法
the color of fruit is green
% apple set color  
0                                         ;#仍然是0,因为在show_color中定义的color只是局部变量(没有使用关键字instvar)

[5] 继承
在OTcl中,使用关键字superclass表示类的继承。
注意:
(1)在OTcl中所有成员变量和成员函数都是基于public属性的(这一点不同于C++),从而减小了编程难度;
(2)子类的对象可以继承和使用基类的所有成员函数和方法;
例如:
% Class Fruits                               ;#定义父类Fruits

Fruits instproc grow{} {                     ;#定义Fruits的成员函数grow
 $self instvar weight
 incr weight
 if{$weight>10} then{
 puts stderr "it is mature!"
 }
 return {}                                            ;#注意return的用法与C中的区别
}

% Fruits instproc show_color{} {         ;#定义Fruits的成员函数show_color
   set color green
   puts "the color of fruit is $color"
}

% Class Apple -superclass Fruits         ;#类Apple继承自Fruits
% Apple apple
  Apple instproc init{args} {
     eval $self next $args                      ;#调用父类Fruits的构造函数
}
% apple show_color                            ;#实际调用的是父类Fruits的show_color函数
the color of fruit is green

[6] 多继承
P.34
例如:
Class 子类 -superclass {父类1 父类2} ;#多继承
注意:
(1)可以用info命令查看类的继承关系;
例如:
% 子类 info heritage    ;#查看子类的继承关系
父类1 父类2 Object      ;#按顺序输出子类继承的父类

[7] 重写
子类可以重写父类的成员函数,在成员函数中使用next命令来覆盖父类中同名的成员函数。

[8] 常用关键字
名称        描述                        类型
-------------------------------------------------
self        对象本身                   变量
proc       方法名                      变量
class      定义类                      变量
next     调用父类的同名方法      方法

The Network Simulator, Version 3 -------------------------------- Table of Contents: ------------------ 1) An overview 2) Building ns-3 3) Running ns-3 4) Getting access to the ns-3 documentation 5) Working with the development version of ns-3 Note: Much more substantial information about ns-3 can be found at http://www.nsnam.org 1) An Open Source project ------------------------- ns-3 is a free open source project aiming to build a discrete-event network simulator targeted for simulation research and education. This is a collaborative project; we hope that the missing pieces of the models we have not yet implemented will be contributed by the community in an open collaboration process. The process of contributing to the ns-3 project varies with the people involved, the amount of time they can invest and the type of model they want to work on, but the current process that the project tries to follow is described here: http://www.nsnam.org/developers/contributing-code/ This README excerpts some details from a more extensive tutorial that is maintained at: http://www.nsnam.org/documentation/latest/ 2) Building ns-3 ---------------- The code for the framework and the default models provided by ns-3 is built as a set of libraries. User simulations are expected to be written as simple programs that make use of these ns-3 libraries. To build the set of default libraries and the example programs included in this package, you need to use the tool 'waf'. Detailed information on how use waf is included in the file doc/build.txt However, the real quick and dirty way to get started is to type the command ./waf configure --enable-examples followed by ./waf in the the directory which contains this README file. The files built will be copied in the build/ directory. The current codebase is expected to build and run on the set of platforms listed in the RELEASE_NOTES file. Other platforms may or may not work: we welcome patches to improve the portability of the code to these other platforms. 3) Running ns-3 --------------- On recent Linux systems, once you have built ns-3 (with examples enabled), it should be easy to run the sample programs with the following command, such as: ./waf --run simple-global-routing That program should generate a simple-global-routing.tr text trace file and a set of simple-global-routing-xx-xx.pcap binary pcap trace files, which can be read by tcpdump -tt -r filename.pcap The program source can be found in the examples/routing directory. 4) Getting access to the ns-3 documentation ------------------------------------------- Once you have verified that your build of ns-3 works by running the simple-point-to-point example as outlined in 4) above, it is quite likely that you will want to get started on reading some ns-3 documentation. All of that documentation should always be available from the ns-3 website: http:://www.nsnam.org/documentation/. This documentation includes: - a tutorial - a reference manual - models in the ns-3 model library - a wiki for user-contributed tips: http://www.nsnam.org/wiki/ - API documentation generated using doxygen: this is a reference manual, most likely not very well suited as introductory text: http://www.nsnam.org/doxygen/index.html 5) Working with the development version of ns-3 ----------------------------------------------- If you want to download and use the development version of ns-3, you need to use the tool 'mercurial'. A quick and dirty cheat sheet is included in doc/mercurial.txt but reading through the mercurial tutorials included on the mercurial website is usually a good idea if you are not familiar with it. If you have successfully installed mercurial, you can get a copy of the development version with the following command: "hg clone http://code.nsnam.org/ns-3-dev"
最新发布
03-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值