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

VB.Net - 类与对象

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

由 yiyohunter 创建,youj 最后一次修改 2016-12-12 定义类时,可以为数据类型定义蓝图。 这实际上并不定义任何数据,但它定义了类名的含义,即类的对象将包含什么以及可以对这样的对象执行什么操作。对象是类的实例。 构成类的方法和变量称为类的成员。 类的定义类的定义以关键字Class开头,后跟类名称; 和类体,由End Class语句结束。 以下是类定义的一般形式: [ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _Class name [ ( Of typelist ) ] [ Inherits classname ] [ Implements interfacenames ] [ statements ]End Classattributelist 属性列表: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类,它有三个数据成员,长度,宽度和高度: Module mybox Class Box Public length As Double ' Length of a box Public breadth As Double ' Breadth of a box Public height As Double ' Height of a box End Class Sub Main() Dim Box1 As Box = New Box() ' Declare Box1 of type Box Dim Box2 As Box = New Box() ' Declare Box2 of type Box Dim volume As Double = 0.0 ' Store the volume of a box here ' box 1 specification Box1.height = 5.0 Box1.length = 6.0 Box1.breadth = 7.0 ' box 2 specification Box2.height = 10.0 Box2.length = 12.0 Box2.breadth = 13.0 'volume of box 1 volume = Box1.height * Box1.length * Box1.breadth Console.WriteLine("Volume of Box1 : {0}", volume) 'volume of box 2 volume = Box2.height * Box2.length * Box2.breadth Console.WriteLine("Volume of Box2 : {0}", volume) Console.ReadKey() End SubEnd Module当上述代码被编译和执行时,它产生了以下结果: Volume of Box1 : 210Volume of Box2 : 1560成员函数和封装类的成员函数是一个函数,它的定义或其原型在类定义中像任何其他变量一样。 它对它所属的类的任何对象进行操作,并且可以访问该对象的类的所有成员。成员变量是对象的属性(从设计角度),它们被保持为私有以实现封装。 这些变量只能使用public成员函数访问。让我们把上面的概念设置并获得类中不同类成员的值: Module mybox Class Box Public length As Double ' Length of a box Public breadth As Double ' Breadth of a box Public height As Double ' Height of a box Public Sub setLength(ByVal len As Double) length = len End Sub Public Sub setBreadth(ByVal bre As Double) breadth = bre End Sub Public Sub setHeight(ByVal hei As Double) height = hei End Sub Public Function getVolume() As Double Return length * breadth * height End Function End Class Sub Main() Dim Box1 As Box = New Box() ' Declare Box1 of type Box Dim Box2 As Box = New Box() ' Declare Box2 of type Box Dim volume As Double = 0.0 ' Store the volume of a box here ' box 1 specification Box1.setLength(6.0) Box1.setBreadth(7.0) Box1.setHeight(5.0) 'box 2 specification Box2.setLength(12.0) Box2.setBreadth(13.0) Box2.setHeight(10.0) ' volume of box 1 volume = Box1.getVolume() Console.WriteLine("Volume of Box1 : {0}", volume) 'volume of box 2 volume = Box2.getVolume() Console.WriteLine("Volume of Box2 : {0}", volume) Console.ReadKey() End SubEnd Module当上述代码被编译和执行时,它产生了以下结果: Volume of Box1 : 210Volume of Box2 : 1560构造函数和析构函数类构造函数是每当我们创建该类的新对象时执行的类的特殊成员子类。 构造函数具有名称New,并且没有任何返回类型。下面的程序解释了构造函数的概念: Class Line Private length As Double ' Length of a line Public Sub New() 'constructor Console.WriteLine("Object is being created") 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 l

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


VB.Net - 类与对象