In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about VB. NET abstract class how to use content. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Creating abstract classes
The definition of abstract classes is simple, as long as the keyword MustInherit is added to the domain, such as:
Public MustInherit
class MyAbstract class
...
End class
Abstract class definitions are no different from non-abstract class definitions except that they include keywords. Abstract classes contain properties and methods inherited from other classes. But how do class members (attributes and methods) defined in abstract classes relate to defining elements in derived classes? There are no complicated rules here, but the decision you make is closely related to the properties of the class you own and the classes it will spawn from.
You have three choices.
1, VB. NET abstract class in the existing functions such as attributes, methods, etc. can be overloaded in derived classes. To do this, enter the Overridable keyword in the member definition. For example:
Public Overridable
Sub Move(NewX As Integer,
NewY As Integer)
...
End Sub
2. It is a member of the overloaded abstract base class, and the derived class must use the Overrides key field in the execution statement. For example:
Public Overrides Move
(NewX As Integer,
NewY As Integer)
...
End Sub
3.Functions existing in VB. NET abstract classes cannot be overloaded in derived classes. That is, derived classes must use members of abstract class definitions. This default will result if you define any member in an abstract class without using any special fields. The following code defines the Xpos attribute in an abstract class. Derived classes must use this attribute because it cannot be overloaded. For example:
Public Property XPos()
As Integer
...
End Property
Use the MustOverride keyword to define abstract class members that can be overloaded in derived classes. The following example illustrates how derived classes must execute a method named GetObjectAt().
Public MustOverride Function
GetObjectAt(X As Integer,
Y As Integer) As Object
Note that MustOverride members do not require End Sub or End Function statements. The most convenient way to use MustOverride is that it specifies not only the name of the member but also the symbol of the member. The previous example requires that any derived class define a method named GetObjectAt(), thus generating two types of integer variables and returning a type object. The method is executed in a derived class entirely according to the programmer's instructions. Methods described by the MustOverride field are sometimes referred to as abstract members.
VB. NET abstract classes and interfaces
If you are familiar with interfaces, you would think abstract classes are so similar to them. In fact, it is only partially similar. The definition of an abstract class with only abstract members is basically the same as the definition of an interface, because you have specified that derived classes must execute a member with a special symbol. In addition, abstract classes have other functions that interfaces cannot implement--especially in defining the basic functions of non-abstract class members. BR>
When you think that abstract classes or interfaces will work as usual, keep in mind the following:
In addition to deriving from a class (abstract or non-abstract), a derived class can implement multiple interfaces.
Subclasses derived from abstract classes can also implement one or more interfaces, depending on your project. You can use only one abstract class, multiple interfaces, or a mixture of abstract classes and interfaces. VB and. NET provide a lot of flexibility in this work. So you can achieve your desired outcome in different ways. The bottom line is that some projects must be implemented through abstract classes.VB. NET abstract classes may be considered a very professional approach to programming. It can help programmers reduce workload and can generate simpler and more powerful programs.
Use abstract classes in practice
Imagine you are developing a new employee database for a large company. Your job is to supervise class implementation, align employee profiles with management requirements, and also facilitate differentiation of branch requirements.
The basic requirements are as follows:
Abstract classes have attributes such as name and time of employment, which are not overloaded.
Since most employees are from the United States, retirement ID is implemented in VB. NET abstract class to run social protection numbers. Foreign affiliates will use different ways to identify employee retirement IDs, so this attribute will be overloaded in derivative classes for individual affiliates to implement separately.
A method named compensation is not controversial and returns a type object with employee compensation details. Because different branches have different compensation, salary, commission, bonus, etc., it is necessary to implement these objects flexibly, which leads to an abstract method.
The resulting abstract class code, called Employee Base, is displayed in the *** column.
Programmers at the French branch use the employeeBase class as the base class for the employeeFrance class to work with local employee recording software. Therefore, derived classes require fields such as inherited name and hire date. What's more, the Retirement ID component in the Employee Base class is suitable for use in France, so the new class will not overload that component. All of this work is done by programmers to perform overloading of abstract Compensation members.
However, in the UK, the Retirement ID attribute does not apply there, so derived classes overload this member, as do Compensation members.
Thank you for reading! About "VB. NET abstract class how to use" this article is shared here, I hope the above content can have some help for everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.