VB. NET Object-Oriented Programming features
Visual Basic 7, also known as VB. NET, has all the features of object-oriented programming language (OOP. For VB programmers, the concept of object-oriented and object-oriented programming methods are no stranger.
What is an object-oriented programming language? He may say a lot of terms such as classes, interfaces, message hiding, encapsulation, inheritance, and polymorphism. These terms all sound cool, aren't they? However, object-oriented programming cannot be mastered after one or two days of study or one lesson. To truly master object-oriented programming, you must not only have some theoretical knowledge, but also some practical programming exercises. This article discusses the basic methods of using object-oriented programming in VB. NET, and comprehensively discusses that the knowledge of object-oriented programming in VB. NET is beyond the scope of this article.
Advantages of Object-Oriented Programming
I wonder whether the reader has considered why modern programming languages are moving closer to object-oriented programming? Why is C ++ and JAVA so popular? This is because object-oriented programming has several advantages, such as convenient code maintenance, good scalability, and support for code reuse technology. These advantages are not available in procedural programming languages. Next we will talk about these advantages of object-oriented technology:
Easy to maintain
Modularity is a feature of object-oriented programming. Objects are represented as classes with the same functions in the same namespace. we can add a class in the namespace without affecting other members of the namespace.
Scalability
Object-Oriented Programming essentially supports scalability. If a class has a certain function, you can quickly expand the class and create a class with the extended function.
Code reuse
Because the function is encapsulated in the class and the class exists as an independent entity, it is very easy to provide a class library. In fact, any. NET Framework programming language programmer can use the. NET Framework class library. the. NET Framework Class Library provides many functions. More happily, we can expand these functions by providing classes that meet the requirements.
Next we will discuss some of the features of object-oriented programming with the simplest features.
Class
In object-oriented programming technology, classes are the focus. In short, a class is a data type that provides certain functions. Keyword Class is used to define a Class in VB. NET. For example, the following code defines a Class named "Employee:
Employee class
Class Employee
End Class
Defining a class is that simple. Note: When naming classes, Microsoft recommends the use of Pascal naming rules. According to this naming rule, it means that the first letter of the class name must be capitalized, and the first letter of the followed concurrent link word is capitalized, for example, class names like GeneralManager, SmallDictionary, and StringUtil comply with such rules.
Class Member
A class has members such as domain, attribute, subroutine, and function. For example, the following employee class has a subroutine named work:
The Employee class that contains the Work method
Class Employee
Public Sub Work ()
'Do something here
End Sub
End Class
Subprograms and functions are called methods. Method naming also follows the naming rules of Pascal.
Another type of member is a domain. Domain naming rules follow the camel rules, that is, the first letter of all substrings except the first substring is capitalized. Like salary and quarterlyBonus are domain names that comply with the rules. The following code adds the salary and quarterlyBonus fields to the Employee class:
Added two fields of the Employee class.
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Print the salary to the Console
System. Console. Write (salary)
End Sub
End Class
Module Module1
Public Sub Main ()
Dim anEmployee As Employee
AnEmployee = New Employee ()
AnEmployee. PrintSalary ()
End Sub
End Module
The module of Module1 in the above Code segment provides the Main function of the subroutine, which is also the start of the VB. NET program. To compile the source program, you must use one or more methods to access the Main Sub.
If you are not using Visual studio.net, you can use vbc.exesoftware to compile the vb.netsource program. vbc.exe is automatically installed when installing the. NET Framework. For example, if you save the source code as the file "Employee. vb", enter "vbc Employee. vb" in the directory where Employee. vb is located to compile the source program.
Now let's take a look at the above Code. The Main function of the subroutine first defines a variable of the Employee type:
Dim anEmployee As Employee
Then, use the New keyword to initialize the Employee:
AnEmployee = New Employee ()
In this way, we get a variable of the Employee type, and we can use its function (after the engineer of the Ford Motor Company produces the car, we can start and drive it .). In our example, you can use the following method to call the PrintSalary method:
AnEmployee. PrintSalary ()
This method prints the value of the salary variable in the Employee.
Of course, we can also move the Main function of the subprogram to the class definition, so that we don't need to use the module any more. The following code demonstrates this method: the Main function of the subroutine is defined in the class.
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Print the salary to the Console
System. Console. Write (salary)
End Sub
Public Shared Sub Main ()
Dim employee As Employee
Employee = New Employee ()
Employee. PrintSalary ()
End Sub
End Class
Note: System. Console. Write in the PrintSalary method indicates that the Write method in the Console class is called, and the Console class is part of the System namespace. The Essentials of namespaces are discussed in the following sections:
Namespace
Classes and other types are used when writing. NET software. To make applications more organized, classes can be combined into namespaces, which is the same for Microsoft's. NET Framework class libraries. If.. NET Framework SDK documentation. NET Framework Class Library, you will see more than 80 namespace, frequently and even important namespace including System, System. IO, System. drawing, System. windows. forms. For example, in the PrintSalary method of the Employee class, we use the Console class in the System namespace.
If you want to use a namespace in a program, you can first import it so that you do not need to repeat the name of The namespace each time you use its members. For example, you can rewrite the code in table 4 and table 5 to the form in table 6 below:
Import namespace
Imports System
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Print the salary to the Console
Console. Write (salary)
End Sub
Public Shared Sub Main ()
Dim employee As Employee
Employee = New Employee ()
Employee. PrintSalary ()
End Sub
End Class
Now we can use the Console class in the PrintSalary method without referencing the namespace, because we have imported this namespace.
We can also have classes with the same name in different namespaces. To correctly use a class, we usually use the namespace name before a class. For example, you can use the Console class in the System namespace as System. Console.
Access type
In many cases, we provide compiled classes to others for them to use their functions. For example, they can call a method of a class or access a domain. One of the biggest advantages of object-oriented programming is that developers can easily control access to class members, which means that we can fully control what we want others to use. We can enable a method to be used by other developers, or allow a class member to be accessed only in the class. In VB. NET, access is classified. We will discuss these levels below:
PPublic: Public class members have no access restrictions. Add a Public key before a class member so that it can be accessed at will. For example, the PrintSalary method in the Employee class is a public method that can be accessed from anywhere.
Private: secret class members can only be accessed by other Members in the class. You can use the Private key to make a class member secret.
Protected: Protected class members can only be accessed by the class's derived classes and the class itself. You can use the Protected parameter to make the class member a Protected class member.
Friend: class members with access restrictions at the friend level can only be used within the program that defines the class. Using the Friend word can make the class members have access restrictions at the friend level.
Protected friend: A combination of protected and friend access types. These different access types enable information hiding capabilities for object-oriented programming. That is to say, we can use these access types to protect information that is unwilling to be accessed by others.
Static Member
Let's take a look at the Employee class in table 4, 5, and 6. Maybe the reader will not. the Console class is instantiated and Its Write is a bit incomprehensible. Why can we do this? In object-oriented programming languages, there is a special class member called static members. VB. NET also has the concept of static members.
You can use static members without instantiating an object. For example, in table 7 below, the SalaryLevel class only contains static fields:
Class Member
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
We can use classes in the program as demonstrated by the program in table 8:
Listing 8: Using a static member of a class
Imports System
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
Class Employee
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Use the static domain of the SalaryLevel class to output the salary to the Console
Console. Write (SalaryLevel. Level4)
End Sub
Public Shared Sub Main ()
Dim employee As Employee
Employee = New Employee ()
Employee. PrintSalary ()
End Sub
End Class
In the PrintSalary method of the Employee class, we can use the static domain Level4 without creating the SalaryLevel class variable first. A class member that does not belong to a static member is called an instance Member.
Constructor
The A constructor is A special method required for class initialization. In VB. NET, this method is called New. However, in the previous code, we can find that the New method is not defined in the class. In this way, if no constructor is defined in the class, VB. NET will automatically create a constructor. When the object is initialized using the New feature, the constructor of the class is called. Of course, we can also write the code that the object runs during initialization.
If we create a constructor in the program, VB. NET will no longer automatically create constructor for this class.
Inheritance
Inheritance is a feature of extended classes. If you need to complete some functions, you can certainly create a new class, but if the classes created by others can provide some of the functions you need, you can create a new class that expands the original class, the class we created can be called a subclass or a derived class. The original class can be called a base class or a parent class. Sometimes, subclass and inheritance are also used to describe the extension of classes. In VB. NET, a class can only inherit one parent class. multi-class inheritance is not allowed in VB. NET.
In syntax, a colon is added after the class name, followed by the name of the Inherits and parent class to inherit the class. For example, the Code in table 9 below creates a new class called Manager by extending the Employee class:
Extended class
Imports System
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Print the salary to the Console
Console. Write (salary)
End Sub
End Class
Class Manager: Inherits Employee
End Class
If the word Guan Jian appears on the next line, the semicolon next to the subclass name is not required, as shown in the following code:
Class Manager
Inherits Employee
End Class
Now, we can initialize a Manager object and use the members in the Employee. As shown in the code in table 10 below:
Initialize the Manager object
Class Employee
Public salary As Decimal = 40000
Public yearlyBonus As Decimal = 4000
Public Sub PrintSalary ()
'Print the salary to the Console
Console. Write (salary)
End Sub
End Class
Class Manager: Inherits Employee
End Class
Module Module1
Public Sub Main ()
Dim manager As Manager
Manager = New Manager ()
Manager. PrintSalary ()
End Sub
End Module
The code in table 11 below demonstrates how to expand the Manager category by writing a new PrintBonus method:
Add a new method to the subclass
Class Manager: Inherits Employee
Public Sub PrintBonus ()
Console. Write (yearlyBonus)
End Sub
End Class
Note the use of accessible members. For example, if the yearlyBonus domain has a private attribute, the Manager class cannot access this attribute. Therefore, compiling such code may cause an error.
Inheritance is a common method in object-oriented programming. In fact, the. NETFramework class library contains many classes inherited from other classes. For example, the Button class in the Windows. Forms namespace is a subclass of the ButtonBase class, And the ButtonBase class itself is a subclass of the Control class. All classes are eventually subclasses of the System. Object class. In the. NET Framework class library, the System. Object class is called a root or super class.
The code in Table 12 demonstrates the powerful inheritance functions:
Expand System. Windows. Forms. Form
Public Class MyForm: Inherits System. Windows. Forms. Form
End Class
This is an empty class definition. When compiled and run, a Windows form is displayed. You can create a Form without writing a line of code. This is because MyForm is generated by System. Windows. Forms. Form, which inherits the functions of the Form class.
Class that cannot be inherited
We can use NotInheritable to disable the inheritance of our class. For example, Calculator in Table 13 cannot be inherited:
Class that cannot be inherited
NotInheritable Class Calculator
End Class
If this class is expanded, compilation errors will occur. Why can't our classes be inherited? One reason is that we do not want others to expand our classes, and the other reason is that non-expandable classes produce faster code. Even so, we should be careful to use non-inherited classes because they do not conform to the original intention of object-oriented programming. They can be inherited only when they are certainly not extended by 100%.
In some object-oriented programming languages, these classes are also called final classes.