Object-oriented programming feature in VB.NET

zhaozj2021-02-08  496

Object-oriented programming feature in VB.NET (this article reprinted from Software Engineering Expert Network www.21cmm.com, does not mean Gigix point) Visual Basic 7 is also known as VB.NET, with object-oriented programming language All features. For VB programmers, object-oriented concepts and object-oriented programming methods are not strange. If you ask an object-oriented programming master what is an object-oriented programming language? He may say a big pile such as a noun such as class, interface, message, package, inheritance, and polymorphism. These nouns sound cool, isn't it? However, object-oriented programming is not possible to learn or listen to one or two days. To truly master the object-oriented programming, not only need to master certain theoretical knowledge, but also conduct some actual programming exercises. This paper discusses the basic method for transporting object-oriented principles programming in VB.NET, and comprehensive discussion of object-oriented programming in VB.NET has exceeded the scope of this article. Object-oriented programming, don't know if readers think about why modern programming language will be close to object-oriented programming? C , why is Java so popular? This is because object-oriented programming has several advantages, such as: Code maintenance, good scalability, support code reuse technology, etc. These advantages are not available in process programming languages. Let's talk about these advantages for object-oriented technologies: Maintenance Simple modularization is a feature of object-oriented programming. Entities are expressed as classes with the same function as the class and the same name space, we can add a class to other members of the name space without affecting the namespace. Object-oriented programming is essentially supported. If there is a class with a function, you can quickly expand this class, create a class with an extended function. The code is reused because the function is packaged in the class, and the class is existing as a separate entity, providing a class library is very simple. In fact, the programmer of any .NET Framework programming language can use the .NET Framework class library, .NET Framework class library provides a lot of features. What is even more pleased, we can expand these features by providing a class that meets the needs. Below our simplest feature begins to discuss some features of object-oriented programming. Class is in object-oriented programming technology, the class is the focus of the focus. Simply put, the class is a type of data that provides a certain function. Define a class in VB.NET To use a keyword class, for example, a small section below defines a class named Employee: Employee class Class Employee End Class Defines a class is as simple as it is. Note that Microsoft recommends Naming Rules for the Pascal language when named. According to this naming rule, it means that the first letter of the name must be capitalized, and the first letter of the back and closet is uppercase, such as the generalmanager, smallDictionary, Stringutil is a class name of this rule. . A class member is an iconic domain, attribute, subroutine, and function. For example, there is a subroutine named Work in the following EMPLOYEE class: Employee Class Employee Public Sub Work () "Do Something Here" with the work method. End Sub End Class subroutines and functions are called methods, and the naming of the method also follows the naming rules of the Pascal language. Another kind of member is a domain. The domain nomenclature follows the Camel rules, that is, in addition to the first letter of all substrings outside the first substruction. Like Salary and Quarterlybonus are domain names that meet the rules.

Add the following code in the Employee class quarterlyBonus salary and these two fields: an increase of the Employee class Class Employee Dim salary two domains 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 sub provided () Dim anEmployee As Employee anEmployee = New Employee () in anEmployee.PrintSalary () End sub End module code segment Module1 module The main function of the program, which is also where the VB.NET program starts. To compile the source program, you must use one or another way to access the main SUB. If you are not Visual Studio.net, you can compile VB.NET source using VBC.exe software, and VBC.exe is automatically installed when you install .NET Framework. For example, after you save the source code as an Employee.vb file, enter VBC Employee.vb in the directory where employee.vb is located, you can compile the source program. Now let's take a look at the code above, the main function of the subroutine first defines a variable of an Employee type. Just get an Employee type variable, we can use its function (after the engineer of Ford Motor produces the car, we can start and drive it.). In our example, you can call the printsalary method using the following method: Anemployee.printsalary () This method prints the value of the Salary variable in EMPLOYEE. Of course, we can also move the MAIN function of the subroutine to the definition of the class, so there is no need to use the module. The following code demonstrates this method: the main function of the subroutine Class Employee Dim Salary As Decimal = 4000 Public Sub Printsa 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 Notes: PrintSalary method System.Console.Write means that we call Console class The Write method, and the Console class is part of the SYSTEM name space. The essentials about the name space will be discussed below: Name Space When writing .NET software, we will use the class and other types.

In order to make the application more rationally, the class combination can be combined into the namespace, and Microsoft's .NET Framework class library is like this. If you open the .NET Framework Class Library in the .NET Framework SDK document, you will see more than 80 namespaces, often important namespaces that require often and even the important namespace include System, System.io, System.drawing, System.Windows.Forms. Wait. For example, in the PRINTSAlary method of the Employee class, we use the console class in the System namespace. If you want to use the namespace in the program, you can first import it so that you don't need to repeat the name of the name space each time you use its member. For example, you can rewrite the code in Table 4, 5 as in Table 6 below: Imported Names Space Imports System Class Employee Dim Salry 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.PRINTSALARYEE.PRINTSAlary () End Sub End Class is OK, we can now use the Console class in the Printsalary method and no reference name Space because we have imported this name space. We can also have the same name in different namespaces. To properly use a class, the usual approach is to use the namespace name in front of a class. For example, you can use the CONSOLE class in the System.Console using the console class in the System namespace. Access types In many cases, we will provide a good class to others for them to use the features it provides, for example, they can call a method of classes or access one of them. One of the biggest benefits of object-oriented programming is that developers can easily control access to class members, which means we can completely control the part that wants others to use. We can make a method can be used by other developers, or a class member can only be accessed in this class. In VB.NET, access is a grade. Let's discuss these levels: PPUBLIC: Public class members have no access restrictions. Adding a public health word in front of a class member allows it to be casualfully accessed. 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 within this class. You can make a class member secret. Protected: The protected class member can only be accessed by the derived class of such class and the inheid. Use protected to get the class to make the class member a protected class member. Friend: The class member with Friend level access restrictions can only be used inside the program that defines the program, allowing the class member to have Friend level access restrictions. Protected Friend: This is a combination of Protected and Friend two access types. These different access types make the object-oriented programming capabilities. That is, we can use these access types to protect information that is unwilling to access others.

Static members Let's take a look at the Employee class in Table 4, 5, 6, perhaps the reader will not understand the use of the System.Console class, why can we do this? Because in object-oriented programming languages, there is a special class member called static member, VB.NET also has a still member of this concept. You can use the static members without instantification of an object. For example, in Table 7 below, only static domains are included in the Salarylevel class: the state of the class member class Salarylevel Public Shared Level1 As Decimal = 35000 public shared level as decimal = 40000 public Shared Level3 as decimal = 45000 public shared level 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 as used Genre program table 8 demonstrated in the program: 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 Share d Level10 As Decimal = 80000 End Class Class Employee Dim yearlyBonus As Decimal = 4000 Public Sub PrintSalary () 'using a static class field SalaryLevel Console output to wage 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 in the case where you don't first create a SalaryLevel class variable. A class member that is not a static member is called an instance member.

The constructor A constructor is a special method necessary for class initialization, in VB.NET, this method is referred to as New. But we can find in the previous code, we don't define this method in the class. This is the case, if the constructor is not defined in the class, VB.NET will automatically create a constructor, and the class constructor is called when the object is initialized using the Guanjian NEW. Of course, we can also write the code that the object runs in initialization. If we create a constructor in the program, VB.NET will not automatically create a conformation for the class. Inheritance inheritance is a feature of the extension class. If you need to complete some features, you can create a new class, but if the class created by others can provide a part you need, you can create a new class that expands the original class, and the class we created can be called Class or derived class, the original class can be referred to as a basic class or parent class. Sometimes, subclasses and inherits are also used to describe the extension of class. In VB.NET, a class can only inherit a parent class, and multiple types of inherits are not allowed in VB.NET. From the grammar, add a colon after the class name, then add the names of the keyword inherits and the parent class. For example, the code in Table 9 below creates a new class called Manager by expanding the Employee class: expansion imports system class employee dim sales as decimal = 40000 Dim Yearlybonus as decimal = 4000 public subprintsalary () 'Print THE Salary to the console.write (Salary) End subunle consarch manager: inherits Employee End Class If the authenticity word appears in the next row, the semicolon behind 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 members in Employee. Code 10 as shown in the following Table: Initialization 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 module module1 public Sub main () Dim Manager as manager = new manager () manager.printsalary () End Sub End Sprintsalary () End Sub End Module Demo how to expand how to write a new Printbonus method Method: Adding a new method in subclass Class Manager: inherits Employee Public Sub Printbonus () Console.Write (YearlyBonus) End Sub End Class Note that member accessibility limits.

转载请注明原文地址:https://www.9cbs.com/read-612.html

New Post(0)