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

VB.Net - 基本语法

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

由 yiyohunter 创建,youj 最后一次修改 2016-12-12 VB.Net是一种面向对象的编程语言。 在面向对象编程方法中,程序由通过动作相互交互的各种对象组成。 对象可能采取的动作称为方法。 相同类型的对象被认为具有相同的类型,或者更经常地被称为在同一类中。 当我们考虑VB.Net程序时,它可以定义为通过调用对方的方法进行通信的对象的集合。 现在让我们简单地看看类,对象,方法和实例变量是什么意思。 Object 对象 -对象具有状态和行为。 示例:狗有状态 - 颜色,名称,品种以及行为 - 摇摆,吠叫,吃饭等。对象是类的实例。Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating, etc. An object is an instance of a class. Class 类 -类可以被定义为描述其类型的对象支持的行为/状态的模板/蓝图。A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support. Methods 方法 -方法基本上是一种行为。 一个类可以包含许多方法。 它是在写逻辑,操纵数据和执行所有动作的方法中。A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed. 实例变量 -每个对象都有其唯一的实例变量集。 对象的状态由分配给这些实例变量的值创建。Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables. VB.Net中的Rectangle类例如,让我们考虑一个Rectangle对象。 它具有长度和宽度等属性。 根据设计,它可能需要接受这些属性的值,计算面积和显示细节的方式。 让我们看一个Rectangle类的实现,并在我们的观察的基础上讨论VB.Net基本语法: Imports SystemPublic 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 SubEnd Class当上述代码被编译和执行时,它产生以下结果: Length: 4.5Width: 3.5Area: 15.75在上一章中,我们创建了一个包含代码的Visual Basic模块。 Sub Main表示VB.Net程序的入口点。 这里,我们使用包含代码和数据的类。 您使用类来创建对象。 例如,在代码中,r是一个Rectangle对象。 对象是类的一个实例: Dim r As New Rectangle()类可以具有可以从外部类访问的成员,如果指定的话。 数据成员称为字段,过程成员称为方法。可以在不创建类的对象的情况下调用共享方法或静态方法。 通过类的一个对象调用实例方法: Shared Sub Main() Dim r As New Rectangle() r.Acceptdetails() r.Display() Console.ReadLine()End Sub标识符标识符是用于标识类,变量,函数或任何其他用户定义项的名称。 在VB.Net中命名类的基本规则如下: 名称必须以字母开头,后跟一个字母,数字(0 - 9)或下划线。 标识符中的第一个字符不能是数字。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关键字下表列出了VB.Net保留的关键字: AddHandlerAddressOfAliasAndAndAlsoAsBooleanByRefByteByValCallCaseCatchCBoolCByteCCharCDateCDecCDblCharCIntClassCLngCObjConstContinueCSByteCShortCSngCStrCTypeCUIntCULngCUShortDateDecimalDeclareDefaultDelegateDimDirectCastDoDoubleEachElseElseIfEndEnd IfEnumEraseErrorEventExitFalseFinallyForFriendFunctionGetGetTypeGetXMLNamespaceGlobalGoToHandlesIfImplementsImportsInInheritsIntegerInterfaceIsIsNotLetLibLikeLongLoopMeModModuleMustInheritMustOverrideMyBaseMyClassNamespaceNarrowingNewNextNotNothingNotInheritableNotOverridableObjectOfOnOperatorOptionOptionalOrOrElseOverloadsOverridableOverridesParamArrayPartialPrivatePropertyProtectedPublicRaiseEventReadOnlyReDimREMRemoveHandlerResumeReturnSByteSelectSetShadowsSharedShortSingleStaticStepStopStringStructureSubSyncLockThenThrowToTrueTryTryCastTypeOfUIntegerWhileWideningWithWithEventsWriteOnlyXor  

VB.Net - 基本语法