课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 类与对象

当前位置:免费教程 » 程序设计 » VB.Net
定义类时,可以为数据类型定义蓝图。 这实际上并不定义任何数据,但它定义了类名的含义,即类的对象将包含什么以及可以对这样的对象执行什么操作。

对象是类的实例。 构成类的方法和变量称为类的成员。


类的定义

类的定义以关键字Class开头,后跟类名称; 和类体,由End Class语句结束。 以下是类定义的一般形式:

  1. [ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _
  2. Class name [ ( Of typelist ) ]
  3. [ Inherits classname ]
  4. [ Implements interfacenames ]
  5. [ statements ]
  6. End Class

  • attributelist 属性列表:is a list of attributes that apply to the class. Optional.  attributelist是一个适用于类的属性列表。 可选的。

  • accessmodifier 访问修改器:defines the access levels of the class, it has values as - Public, Protected, Friend, Protected Friend and Private. Optional.  accessmodifier定义类的访问级别,它的值为 - Public,Protected,Friend,Protected Friend和Private。 可选的。

  • Shadows 阴影:indicate that the variable re-declares and hides an identically named element, or set of overloaded elements, in a base class. Optional.  阴影表示变量在基类中重新声明和隐藏一个同名的元素或一组重载的元素。 可选的。

  • MustInherit:specifies that the class can be used only as a base class and that you cannot create an object directly from it, i.e., an abstract class. Optional.  MustInherit指定该类只能用作基类,并且不能直接从它创建对象,即抽象类。 可选的。

  • NotInheritable 不可继承:specifies that the class cannot be used as a base class.  NotInheritable指定该类不能用作基类。

  • Partial 部分:indicates a partial definition of the class.   Partial表示类的部分定义。

  • Inherits 继承:specifies the base class it is inheriting from.  Inherits指定它继承的基类。

  • Implements 实现:specifies the interfaces the class is inheriting from.  Implements指定类继承的接口。


下面的示例演示了一个Box类,它有三个数据成员,长度,宽度和高度:

  1. Module mybox
  2. Class Box
  3. Public length As Double ' Length of a box
  4. Public breadth As Double ' Breadth of a box
  5. Public height As Double ' Height of a box
  6. End Class
  7. Sub Main()
  8. Dim Box1 As Box = New Box() ' Declare Box1 of type Box
  9. Dim Box2 As Box = New Box() ' Declare Box2 of type Box
  10. Dim volume As Double = 0.0 ' Store the volume of a box here
  11. ' box 1 specification
  12. Box1.height = 5.0
  13. Box1.length = 6.0
  14. Box1.breadth = 7.0
  15. ' box 2 specification
  16. Box2.height = 10.0
  17. Box2.length = 12.0
  18. Box2.breadth = 13.0
  19. 'volume of box 1
  20. volume = Box1.height * Box1.length * Box1.breadth
  21. Console.WriteLine("Volume of Box1 : {0}", volume)
  22. 'volume of box 2
  23. volume = Box2.height * Box2.length * Box2.breadth
  24. Console.WriteLine("Volume of Box2 : {0}", volume)
  25. Console.ReadKey()
  26. End Sub
  27. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Volume of Box1 : 210
  2. Volume of Box2 : 1560


成员函数和封装

类的成员函数是一个函数,它的定义或其原型在类定义中像任何其他变量一样。 它对它所属的类的任何对象进行操作,并且可以访问该对象的类的所有成员。

成员变量是对象的属性(从设计角度),它们被保持为私有以实现封装。 这些变量只能使用public成员函数访问。

让我们把上面的概念设置并获得类中不同类成员的值:

  1. Module mybox
  2. Class Box
  3. Public length As Double ' Length of a box
  4. Public breadth As Double ' Breadth of a box
  5. Public height As Double ' Height of a box
  6. Public Sub setLength(ByVal len As Double)
  7. length = len
  8. End Sub
  9. Public Sub setBreadth(ByVal bre As Double)
  10. breadth = bre
  11. End Sub
  12. Public Sub setHeight(ByVal hei As Double)
  13. height = hei
  14. End Sub
  15. Public Function getVolume() As Double
  16. Return length * breadth * height
  17. End Function
  18. End Class
  19. Sub Main()
  20. Dim Box1 As Box = New Box() ' Declare Box1 of type Box
  21. Dim Box2 As Box = New Box() ' Declare Box2 of type Box
  22. Dim volume As Double = 0.0 ' Store the volume of a box here
  23.  
  24. ' box 1 specification
  25. Box1.setLength(6.0)
  26. Box1.setBreadth(7.0)
  27. Box1.setHeight(5.0)
  28. 'box 2 specification
  29. Box2.setLength(12.0)
  30. Box2.setBreadth(13.0)
  31. Box2.setHeight(10.0)
  32. ' volume of box 1
  33. volume = Box1.getVolume()
  34. Console.WriteLine("Volume of Box1 : {0}", volume)
  35.  
  36. 'volume of box 2
  37. volume = Box2.getVolume()
  38. Console.WriteLine("Volume of Box2 : {0}", volume)
  39. Console.ReadKey()
  40. End Sub
  41. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Volume of Box1 : 210
  2. Volume of Box2 : 1560


构造函数和析构函数

类构造函数是每当我们创建该类的新对象时执行的类的特殊成员子类。 构造函数具有名称New,并且没有任何返回类型。

下面的程序解释了构造函数的概念:

  1. Class Line
  2. Private length As Double ' Length of a line
  3. Public Sub New() 'constructor
  4. Console.WriteLine("Object is being created")
  5. End Sub
  6. Public Sub setLength(ByVal len As Double)
  7. length = len
  8. End Sub
  9. Public Function getLength() As Double
  10. Return length
  11. End Function
  12. Shared Sub Main()
  13. Dim line As Line = New Line()
  14. 'set line length
  15. line.setLength(6.0)
  16. Console.WriteLine("Length of line : {0}", line.getLength())
  17. Console.ReadKey()
  18. End Sub
  19. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Object is being created
  2. Length of line : 6


默认构造函数没有任何参数,但如果需要,构造函数可以有参数。 这样的构造函数称为参数化构造函数。 此技术可帮助您在创建对象时为其分配初始值,如以下示例所示:

  1. Class Line
  2. Private length As Double ' Length of a line
  3. Public Sub New(ByVal len As Double) 'parameterised constructor
  4. Console.WriteLine("Object is being created, length = {0}", len)
  5. length = len
  6. End Sub
  7. Public Sub setLength(ByVal len As Double)
  8. length = len
  9. End Sub
  10. Public Function getLength() As Double
  11. Return length
  12. End Function
  13. Shared Sub Main()
  14. Dim line As Line = New Line(10.0)
  15. Console.WriteLine("Length of line set by constructor : {0}", line.getLength())
  16. 'set line length
  17. line.setLength(6.0)
  18. Console.WriteLine("Length of line set by setLength : {0}", line.getLength())
  19. Console.ReadKey()
  20. End Sub
  21. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Object is being created, length = 10
  2. Length of line set by constructor : 10
  3. Length of line set by setLength : 6


析构函数是一个类的特殊成员Sub,只要它的类的对象超出范围,它就被执行。


析构函数名为Finalize,它既不能返回值也不能接受任何参数。 析构函数在释放程序之前释放资源非常有用,比如关闭文件,释放内存等。

析构函数不能继承或重载。

下面的例子解释析构函数的概念:

  1. Class Line
  2. Private length As Double ' Length of a line
  3. Public Sub New() 'parameterised constructor
  4. Console.WriteLine("Object is being created")
  5. End Sub
  6. Protected Overrides Sub Finalize() ' destructor
  7. Console.WriteLine("Object is being deleted")
  8. End Sub
  9. Public Sub setLength(ByVal len As Double)
  10. length = len
  11. End Sub
  12. Public Function getLength() As Double
  13. Return length
  14. End Function
  15. Shared Sub Main()
  16. Dim line As Line = New Line()
  17. 'set line length
  18. line.setLength(6.0)
  19. Console.WriteLine("Length of line : {0}", line.getLength())
  20. Console.ReadKey()
  21. End Sub
  22. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Object is being created
  2. Length of line : 6
  3. Object is being deleted


VB.Net类的共享成员

我们可以使用Shared关键字将类成员定义为静态。 当我们将一个类的成员声明为Shared时,意味着无论创建多少个对象,该成员只有一个副本。

关键字“共享”意味着类只存在一个成员实例。 共享变量用于定义常量,因为它们的值可以通过调用类而不创建它的实例来检索。

共享变量可以在成员函数或类定义之外初始化。 您还可以在类定义中初始化共享变量。

您还可以将成员函数声明为共享。 这样的函数只能访问共享变量。 共享函数甚至在创建对象之前就存在。

以下示例演示了共享成员的使用:

  1. Class StaticVar
  2. Public Shared num As Integer
  3. Public Sub count()
  4. num = num + 1
  5. End Sub
  6. Public Shared Function getNum() As Integer
  7. Return num
  8. End Function
  9. Shared Sub Main()
  10. Dim s As StaticVar = New StaticVar()
  11. s.count()
  12. s.count()
  13. s.count()
  14. Console.WriteLine("Value of variable num: {0}", StaticVar.getNum())
  15. Console.ReadKey()
  16. End Sub
  17. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Value of variable num: 3


继承

面向对象编程中最重要的概念之一是继承。 继承允许我们根据另一个类来定义一个类,这使得更容易创建和维护应用程序。 这也提供了重用代码功能和快速实现时间的机会。

在创建类时,程序员可以指定新类应该继承现有类的成员,而不是编写完全新的数据成员和成员函数。 这个现有类称为基类,新类称为派生类。


基类和派生类:

类可以从多个类或接口派生,这意味着它可以从多个基类或接口继承数据和函数。

VB.Net中用于创建派生类的语法如下:

  1. <access-specifier> Class <base_class>
  2. ...
  3. End Class
  4. Class <derived_class>: Inherits <base_class>
  5. ...
  6. End Class


考虑一个基类Shape和它的派生类Rectangle:

  1. ' Base class
  2. Class Shape
  3. Protected width As Integer
  4. Protected height As Integer
  5. Public Sub setWidth(ByVal w As Integer)
  6. width = w
  7. End Sub
  8. Public Sub setHeight(ByVal h As Integer)
  9. height = h
  10. End Sub
  11. End Class
  12. ' Derived class
  13. Class Rectangle : Inherits Shape
  14. Public Function getArea() As Integer
  15. Return (width * height)
  16. End Function
  17. End Class
  18. Class RectangleTester
  19. Shared Sub Main()
  20. Dim rect As Rectangle = New Rectangle()
  21. rect.setWidth(5)
  22. rect.setHeight(7)
  23. ' Print the area of the object.
  24. Console.WriteLine("Total area: {0}", rect.getArea())
  25. Console.ReadKey()
  26. End Sub
  27. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Total area: 35


基类初始化

派生类继承基类成员变量和成员方法。 因此,应该在创建子类之前创建超类对象。 超类或基类在VB.Net中被称为MyBase

以下程序演示了这一点:

  1. ' Base class
  2. Class Rectangle
  3. Protected width As Double
  4. Protected length As Double
  5. Public Sub New(ByVal l As Double, ByVal w As Double)
  6. length = l
  7. width = w
  8. End Sub
  9. Public Function GetArea() As Double
  10. Return (width * length)
  11. End Function
  12. Public Overridable Sub Display()
  13. Console.WriteLine("Length: {0}", length)
  14. Console.WriteLine("Width: {0}", width)
  15. Console.WriteLine("Area: {0}", GetArea())
  16. End Sub
  17. 'end class Rectangle
  18. End Class
  19. 'Derived class
  20. Class Tabletop : Inherits Rectangle
  21. Private cost As Double
  22. Public Sub New(ByVal l As Double, ByVal w As Double)
  23. MyBase.New(l, w)
  24. End Sub
  25. Public Function GetCost() As Double
  26. Dim cost As Double
  27. cost = GetArea() * 70
  28. Return cost
  29. End Function
  30. Public Overrides Sub Display()
  31. MyBase.Display()
  32. Console.WriteLine("Cost: {0}", GetCost())
  33. End Sub
  34. 'end class Tabletop
  35. End Class
  36. Class RectangleTester
  37. Shared Sub Main()
  38. Dim t As Tabletop = New Tabletop(4.5, 7.5)
  39. t.Display()
  40. Console.ReadKey()
  41. End Sub
  42. End Class


当上述代码被编译和执行时,它产生了以下结果:

  1. Length: 4.5
  2. Width: 7.5
  3. Area: 33.75
  4. Cost: 2362.5

VB.Net支持多重继承。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号