我们在很多的编程论坛或者新闻组中可以看到c++的拥护者将vb称为
一个玩具语言(toy language)。其中vb最
被人诟病的是它不真正支持面向对象编程(oop)。
在新的visual basic中,这些都将成为过去。visual basic.net将真
正支持oop。继承(inheritance)、多态
(polymorphism)以及重载(overloading)。当然还不止这些。包括一
些新的数据结构、结构性的错误控制(error
handing)以及线程。这些都将被新的vb所支持。
继承
假设你编写了一个只包含一个函数的名称为baseclass的基本
类:
function getcustomername()
' do some stuff
end function
如果你想建立一个新类,并使用baseclass中的getcustomername
函数。但有不想重写一编代码。那么通过继承,
你可以这样编写新类:
inherits baseclass
function getcustomerid()
' do some stuff
end function
重载
重载就是建立两个具有同样你名称但是参数属性不同的函数的过
程。假设有getcustomid函数根据输入输出用
户的id号码。那么该函数可能有以下两种类型:
function getcustomerid(custname as string) as integer
' 通过用户名获得用户id
end function
function getcustomerid(purch as long) as integer
' 根据购物单号码获得用户id
end function
通过重载,你就可以根据不同类型的输入调用同一个函数。
实际上现在的vb在某种程度上提供了重载的功能,考虑下面的函数:
function getcustomid(vin as variant) as long
getcustomid = vin
end function
你可以以下面两种方式调用
id = getcustomid("123")
或者
id = getcustomid(123)
那么为什么还要在新版本中引进重载呢?这是因为新的visual
basic引进了新的特性:类型安全(default
type safety)。在visual basic.net中,variant类型将不复存在,
详细的介绍看下面关于升级部分的内容。
多态
多态是在一个类中重新定义一个函数的过程。例如你需要建立一
个基于baseclass类的子类,但是又需要重新
编写getcustomername函数,在新版的vb中,你就可以这样写:
inherits baseclass
function getorders()
overrides function getorders()
...
end function
线程
新的visual basic语言结构部分将包含建立多线程,一个线程建
立的范例如下:
set t = new thread(new threadstart
(addressof(baseclass.function1))
从上面的范例还可以看到,visual basic.net中的addressof函
数将真正返回函数的指针。这在进行api调用,
例如回调函数方面将是很有帮助的。
错误处理(error handling)
visual basic.net的另外一个大的改进就是错误处理。visual
basic将提供try…catch…finally结构,就象
delphi中的那样(微软的原文是:just like those "respectable"
languages :-))。例如:
sub safewrite()
try
open "testfile"
...
write #1
catch
kill "testfile"
finally
close #1
end try
end sub
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器