VB.Net – 基本语法

VB.Net – 基本语法


VB.Net 是一种面向对象的编程语言。在面向对象的编程方法中,程序由通过动作相互交互的各种对象组成。对象可能采取的操作称为方法。相同种类的对象被称为具有相同的类型,或者更常见的是,被称为在同一类中。

当我们考虑 VB.Net 程序时,它可以定义为通过调用彼此的方法进行通信的对象的集合。现在让我们简要地看看类、对象、方法和实例变量的含义。

  • 对象– 对象具有状态和行为。例子:一只狗有状态——颜色、名字、品种以及行为——摇摆、吠叫、吃东西等。一个对象是一个类的实例。

  • – 类可以定义为模板/蓝图,描述其类型的对象支持的行为/状态。

  • 方法– 方法基本上是一种行为。一个类可以包含许多方法。在方法中编写逻辑、操作数据并执行所有操作。

  • 实例变量– 每个对象都有其独特的实例变量集。对象的状态由分配给这些实例变量的值创建。

VB.Net 中的矩形类

例如,让我们考虑一个 Rectangle 对象。它具有长度和宽度等属性。根据设计,它可能需要接受这些属性的值、计算面积和显示细节的方法。

让我们看一下 Rectangle 类的实现,并根据我们在其中的观察来讨论 VB.Net 基本语法 –

Imports System
Public Class Rectangle
   Private length As Double
   Private width As Double

   'Public methods
   Public Sub AcceptDetails()
      length = 4.5
      width = 3.5
   End Sub

   Public Function GetArea() As Double
      GetArea = length * width
   End Function
   Public Sub Display()
      Console.WriteLine("Length: {0}", length)
      Console.WriteLine("Width: {0}", width)
      Console.WriteLine("Area: {0}", GetArea())

   End Sub

   Shared Sub Main()
      Dim r As New Rectangle()
      r.Acceptdetails()
      r.Display()
      Console.ReadLine()
   End Sub
End Class

当上面的代码被编译和执行时,它会产生以下结果 –

Length: 4.5
Width: 3.5
Area: 15.75

在上一章中,我们创建了一个包含代码的 Visual Basic 模块。Sub Main 表示VB.Net 程序的入口点。在这里,我们使用包含代码和数据的类。您使用类来创建对象。例如,在代码中,r 是一个 Rectangle 对象。

对象是类的实例 –

Dim r As New Rectangle()

一个类可能有可以从外部类访问的成员,如果这样指定的话。数据成员称为字段,过程成员称为方法。

Shared methods or static methods can be invoked without creating an object of the class. Instance methods are invoked through an object of the class −

Shared Sub Main()
   Dim r As New Rectangle()
   r.Acceptdetails()
   r.Display()
   Console.ReadLine()
End Sub

Identifiers

An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in VB.Net are as follows −

  • A name must begin with a letter that could be followed by a sequence of letters, digits (0 – 9) or underscore. The first character in an identifier cannot be a digit.

  • It must not contain any embedded space or symbol like ? – +! @ # % ^ & * ( ) [ ] { } . ; : ” ‘ / and \. However, an underscore ( _ ) can be used.

  • It should not be a reserved keyword.

VB.Net Keywords

The following table lists the VB.Net reserved keywords −

AddHandler AddressOf Alias And AndAlso As Boolean
ByRef Byte ByVal Call Case Catch CBool
CByte CChar CDate CDec CDbl Char CInt
Class CLng CObj Const Continue CSByte CShort
CSng CStr CType CUInt CULng CUShort Date
Decimal Declare Default Delegate Dim DirectCast Do
Double Each Else ElseIf End End If Enum
Erase Error Event Exit False Finally For
Friend Function Get GetType GetXML Namespace Global GoTo
Handles If Implements Imports In Inherits Integer
Interface Is IsNot Let Lib Like Long
Loop Me Mod Module MustInherit MustOverride MyBase
MyClass Namespace Narrowing New Next Not Nothing
Not Inheritable Not Overridable Object Of On Operator Option
Optional Or OrElse Overloads Overridable Overrides ParamArray
Partial Private Property Protected Public RaiseEvent ReadOnly
ReDim REM Remove Handler Resume Return SByte Select
Set Shadows Shared Short Single Static Step
Stop String Structure Sub SyncLock Then Throw
To True Try TryCast TypeOf UInteger While
Widening With WithEvents WriteOnly Xor

觉得文章有用?

点个广告表达一下你的爱意吧 !😁