Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement base class inheritance, overloading and hiding in VB.NET

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how VB. NET to achieve base class inheritance, overloading, hiding, I hope you have some gains after reading this article, let's discuss it together!

Identify who the base class of the class is by declaring the Inherits keyword. Inherits base class name

Sub New(s as string)'construct, add variable s can require forced assignment' Initialize MyBase.New() 'Note: This sentence should be placed in the ** sentence of sub, and the base class with parameters should be marked End Sub Protected Overrides Sub Finalize()' destruct 'End MyBase.Finalize() End Sub

An Overridable modifier is used in the base class to indicate that a property or method in the base class is allowed to be overridden in its derived class. If it is not identified, it is implicitly identified by the NotOverridable modifier, which is used to remind the compiler that the property or method cannot be overridden.

Overwriting: Rewrite methods or properties of base classes with Overridable identifiers in derived classes with Overrides.

Overloads: Create multiple methods and properties with the same name and different parameter lists, which can adapt to the requirements of different parameter types when called.

Hidden: Substituting the name of a derived class for the name of a base class does not make the name disappear. Shadows mode, applicable to any element type, can also be declared as any element type. When hidden in a derived class decorated with private, its subclass will inherit its base class members.

Tips for hiding base class members in the editor

Public Shadows Base class name 1, Base class name 2,...

Modifiers used to control access to VB. NET base class members

Friend : Available only in the current project

private: Available only in this class

protected: members available in this class and its derivatives

protected friend: Available in the current project and derived classes of this class

public: code outside the class can also be accessed

Default value: public

Whenever an instance of a class is created, the Common Language Runtime (CLR) attempts to execute it if a procedure named New exists in the object. New is a procedure called a constructor that initializes a new object before any other code in the object executes. The New constructor can be used to open files, connect to databases, initialize variables, and handle any other tasks that need to be done before an object can be used.

When creating an instance of a derived class, the Sub New constructor of the base class executes first, and then the constructor in the derived class. This is because the *** line of code in the Sub New constructor calls the constructor of the class immediately above itself in the class hierarchy using the syntax MyBase.New(). Then call the Sub New constructor for each class in the class hierarchy until you reach the constructor for the base class. At this point, the code in the base class constructor executes, followed by the code for each constructor in all derived classes, *** executing the code in the closest derived class. When an object is no longer needed, the CLR calls the object's Finalize method and frees its memory. The Finalize method is called a destructor because it performs cleanup tasks such as saving state information, closing files and connections to databases, and performing other tasks that must be completed before an object can be released.

When you call the Sub Finalize method of a derived class, you first perform any cleanup tasks you need and then explicitly call the Sub Finalize method of its base class using the syntax MyBase.Finalize(). Therefore, the Sub Finalize method runs first from the closest derived class, *** executing code from VB. NET base classes.

VB. NET introduces some statements and modifiers to support inheritance. The following table describes the inherited statements and descriptions of VB. NET base classes:

statement/modifier descriptor

Inherits: Inherits statement--Indicates from which class the current class inherits. Inherits keywords are used only in classes and interfaces

NotInheritable: NotInheritable modifier--prohibited as base class

MustInherit: MustInherit modifier--Indicates that an instance of the current class cannot be created. This class can only be inherited.

Overridable: Overridable modifier--allows a property or method of a class to be overridden. Public method defaults to NotOverridable

Overrides: Overrides modifier--override a property or method of the base class

NotOverridable: NotOverridable modifier (default)- -prohibits a property or method of a class from being overridden

MustOverride: MustOverride modifier--A property or method of the class Override that needs to be inherited. When the MustOverride keyword is used, the method definition includes only Sub, Function, and Property statements. It is important to emphasize that any other statement is not allowed without an End Sub, End Function or End Property statement. Class with MustOverride methods must be declared MustInherit. The default value for the Public method is NotOverridable

Shadows: Shadows modifier--allows reuse of inherited class member names. Shadow does not delete inherited type members of a class, it simply makes all inherited type members unusable in derived classes. Shadow is a class member redeclared in a derived class

Class A Public Sub F() Debug.Print ("Base class cannot be overridden") End Sub Public Overridable Sub G() Debug.Print("Base class can be overridden") End Sub Public Sub H() Debug.Print ("A.H") End Sub End Class B Inherits A 'Public Shadows F As String = "Hide Base Class, Regenerate a " Public Overrides Sub G() Debug.Print(Overwrite) End Sub Public Overloads Sub G(ByVal s As Int32) Debug.Print(Overload) End Sub 'Hide base class H members, no hint in editor, but still available with Public Shadows h As Int16 = "3" End Class Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As New B() Dim a As New A a.F() a.G() a.H() Debug.Print(b.F) b.G() b.G(3) Debug.Print(b.h) End Sub End Class

Output:

Base classes cannot be overridden

Base class rewritable

A.H

hide the base class, regenerate a

rewrite

heavy-duty

3

After reading this article, I believe you have a certain understanding of "VB. NET how to achieve base class inheritance, overloading, hiding". If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report