当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

VB.Net - 类与对象

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:59:05

ine.setLength(6.0) Console.WriteLine("Length of line : {0}", line.getLength()) Console.ReadKey() End SubEnd Class当上述代码被编译和执行时,它产生了以下结果: Object is being createdLength of line : 6默认构造函数没有任何参数,但如果需要,构造函数可以有参数。 这样的构造函数称为参数化构造函数。 此技术可帮助您在创建对象时为其分配初始值,如以下示例所示: Class Line Private length As Double ' Length of a line Public Sub New(ByVal len As Double) 'parameterised constructor Console.WriteLine("Object is being created, length = {0}", len) length = len End Sub Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line(10.0) Console.WriteLine("Length of line set by constructor : {0}", line.getLength()) 'set line length line.setLength(6.0) Console.WriteLine("Length of line set by setLength : {0}", line.getLength()) Console.ReadKey() End SubEnd Class当上述代码被编译和执行时,它产生了以下结果: Object is being created, length = 10Length of line set by constructor : 10Length of line set by setLength : 6 析构函数是一个类的特殊成员Sub,只要它的类的对象超出范围,它就被执行。析构函数名为Finalize,它既不能返回值也不能接受任何参数。 析构函数在释放程序之前释放资源非常有用,比如关闭文件,释放内存等。析构函数不能继承或重载。下面的例子解释析构函数的概念: Class Line Private length As Double ' Length of a line Public Sub New() 'parameterised constructor Console.WriteLine("Object is being created") End Sub Protected Overrides Sub Finalize() ' destructor Console.WriteLine("Object is being deleted") End Sub Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line() 'set line length line.setLength(6.0) Console.WriteLine("Length of line : {0}", line.getLength()) Console.ReadKey() End SubEnd Class当上述代码被编译和执行时,它产生了以下结果: Object is being createdLength of line : 6Object is being deletedVB.Net类的共享成员我们可以使用Shared关键字将类成员定义为静态。 当我们将一个类的成员声明为Shared时,意味着无论创建多少个对象,该成员只有一个副本。关键字“共享”意味着类只存在一个成员实例。 共享变量用于定义常量,因为它们的值可以通过调用类而不创建它的实例来检索。共享变量可以在成员函数或类定义之外初始化。 您还可以在类定义中初始化共享变量。您还可以将成员函数声明为共享。 这样的函数只能访问共享变量。 共享函数甚至在创建对象之前就存在。以下示例演示了共享成员的使用: Class StaticVar Public Shared num As Integer Public Sub count() num = num + 1 End Sub Public Shared Function getNum() As Integer Return num End Function Shared Sub Main() Dim s As StaticVar = New StaticVar() s.count() s.count() s.count() Console.WriteLine("Value of variable num: {0}", StaticVar.getNum()) Console.ReadKey() End SubEnd Class当上述代码被编译和执行时,它产生了以下结果: Value of variable num: 3继承面向对象编程中最重要的概念之一是继承。 继承允许我们根据另一个类来定义一个类,这使得更容易创建和维护应用程序。 这也提供了重用代码功能和快速实现时间的机会。在创建类时,程序员可以指定新类应该继承现有类的成员,而不是编写完全新的数据成员和成员函数。 这个现有类称为基类,新类称为派生类。 基类和派生类: 类可以从多个类或接口派生,这意味着它可以从多个基类或接口继承数据和函数。VB.Net中用于创建派生类的语法如下: <access-specifier> Class <base_class>...End ClassClass <derived_class>: Inherits <base_class>...End Class考虑一个基类Shape和它的派生类Rectangle: ' Base classClass Shape Protected width As Integer Protected height As Integer Public Sub setWidth(ByVal w As Integer) width = w End Sub Public Sub setHeight(ByVal h As Integer) height = h End SubEnd Class' Derived classClass Rectangle : Inherits Shape Public Function getArea() As Integer Return (width * height) End FunctionEnd ClassClass RectangleTester Shared Sub Main() Dim rect As Rectangle = New Rectangle() rect.setWidth(5) rect.setHeight(7) ' Print the area of the object. Console.WriteLine("Total area: {0}", rect.getArea()) Console.ReadKey() End SubEnd Class当上述代码被编译和执行时,它产生了以下结果: Total area: 35基类初始化派生类继承基类成员变量和成员方法。 因此,应该在创建子类之前创建超类对象。 超类或基类在VB.Net中被称为MyBase以下程序演示了这一点: ' Base classClass Rectangle Protected width As Double Protected length As Double Public Sub New(ByVal l As Double, ByVal w As Double) length = l width = w End Sub Public Function GetArea() As Double Return (width * length) End Function Public Overridable Sub Display() Console.WriteLine("Length: {0}", length) Console.WriteLine("Width: {0}", width) Console.WriteLine("Area: {0}", GetArea()) End Sub 'end class Rectangle End Class'Derived classClass Tabletop : Inherits Rectangle Privat

上一页  [1] [2] [3]  下一页


VB.Net - 类与对象