Encapsulation, inheritance, polymorphism, abstraction of the VB.net class

Source: Internet
Author: User
Tags definition class definition constructor inheritance first row
Encapsulation | inheritance

Encapsulation is the merging of methods and variables into a class that represents the range that an object can hold to accomplish a certain task and the operations it can perform.

Inheritance is the ability to generate new classes based on methods and member variables of existing classes

Polymorphism is the ability of an object to change its form as the program executes.

Abstraction is the ability to temporarily ignore specific details of an object.

In a class, the constructor's method makes it easier to initialize the member variables of a class, and when you construct another class from one class with inheritance, there are two sets of constructors that work, that is, the base class constructor and the derived class builder

For example:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region "code generated by the Windows forms Designer"

#End Region
Class Base
Public Sub New ()
MsgBox ("base class running")
End Sub
End Class
Class derived
Inherits Base
Public Sub New ()
MsgBox ("derived class running")
End Sub
End Class

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim s as New derived
End Sub
End Class

If the base class constructor does not require a parameter, and the derived class executes, vb.net automatically calls the base class constructor, but if the base class constructor requires parameters, then the derived class must explicitly call the base class constructor and pass the corresponding arguments to the base class builder.

Because all classes use method new as the constructor name, the derived class cannot simply invoke the method new,vb.net cannot determine the need to invoke the new method, which must precede the keyword MyBase when the derived class invokes method new, such as: MyBase.New

In a derived class constructor, the statement that calls the base class constructor must be placed in the first row, otherwise a syntax error is generated, and if no arguments are required, it can be omitted, but in order to improve the readability of the code, it is usually explicitly invoked MyBase.New

Inheritance and destructor methods

VB.net cannot call a base class destructor as much as a base class constructor function, as in the following example:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region "code generated by the Windows forms Designer"

#End Region
Class A
Public Sub New ()
MsgBox ("Class A new")
End Sub
Protected Overrides Sub Finalize ()
MsgBox ("Class A Finalize")
End Sub
End Class
Class b
Inherits A
Public Sub New ()
MsgBox ("Class B new")
End Sub
Protected Overrides Sub Finalize ()
MsgBox ("Class B Finalize")
Mybase.finalize ()
End Sub
End Class
Class C
Inherits b
Public Sub New ()
MsgBox ("Class C new")
End Sub
Protected Overrides Sub Finalize ()
MsgBox ("Class C Finalize")
Mybase.finalize ()
End Sub
End Class

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim s as New c
End Sub
End Class

All classes in the. NET environment derive from the System.Object class, when overriding a base class function and subroutine, you must place the overrides in the definition of the method, and before the derived class overrides the base class definition, the base class must also declare that its method definition is covered, that is, to put the keyword in the method definition overrides , this method that can be overridden by a derived class is called a virtual function.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.