Vb. Object-oriented programming features in net

Source: Internet
Author: User
Tags class definition class manager constructor extend inheritance naming convention domain visual studio
Programming | Objects Visual Basic 7 is also called vb.net, with all the characteristics of an object-oriented (OOP) programming language. For VB programmers, object-oriented concepts and object-oriented programming methods are not unfamiliar.

If you ask an object-oriented program designer what is an object-oriented programming language? He might say a whole bunch of nouns like class, interface, message concealment, encapsulation, inheritance, polymorphism, and these nouns sound cool, don't they? But object-oriented programming is not mastered by a day or two of learning or listening to a lesson. To really grasp the face to
As a program design, not only need to master a certain theoretical knowledge, but also to carry out some practical programming practice. In this paper, the basic method of object-oriented programming in vb.net is discussed, and the knowledge of object-oriented programming in vb.net is beyond the scope of this paper.

The advantages of object-oriented programming

I wonder if readers have considered why modern programming languages move closer to object-oriented programming. Why is C + +, Java so popular? This is because object-oriented programming has several advantages, such as: Easy maintenance of code, scalability, support code reuse technology, and so on. These advantages are not available in the Process programming language. Let's talk about these advantages of object-oriented technology:

Easy Maintenance

Modularity is a feature of object-oriented programming. An entity is represented as a class with the same functionality as a class in the same namespace, and we can add a class to the namespace without affecting other members of the namespace.

Extensibility

Object-oriented programming supports extensibility in nature. If you have a class with some functionality, you can quickly expand the class to create a class with extended functionality.

Code reuse

Since functionality is encapsulated in a class, and a class exists as a stand-alone entity, providing a class library is straightforward. In fact, any programmer in a. NET Framework programming language can use the. NET Framework class library, and the. NET Framework class library provides many features. More happily, we can extend these capabilities by providing classes that meet the requirements.

Here's our simplest feature to discuss some of the features of object-oriented programming.

Class

In object-oriented programming technology, classes are the focus of the emphasis. Simply put, a class is a type of data that provides a certain amount of functionality. Defining a class in vb.net uses the keyword class, for example, the following little piece of code defines a class named employee:

Employee class

Class Employee

End Class

Defining a class is as simple as that. Note that when naming classes, Microsoft recommends the use of Pascal-language naming conventions. According to this naming convention, it means that the first letter of the class name must be capitalized, and that the first letter of the subsequent concurrency link is uppercase, such as Generalmanager, Smalldictionary, and Stringutil, which are the class names that conform to this rule.

Class Members

A class has members like fields, properties, subroutines, and functions, for example, the following employee class has a subroutine named work:

Employee class containing the work method

Class Employee

Public Sub Work ()

' Do something here

End Sub

End Class

Subroutines and functions are called methods, and the naming of methods follows Pascal's naming conventions.

Another class member is a domain. The naming law of a domain follows the camel rule, that is, the first letter of all substrings except the first substring. Like salary and Quarterlybonus are the domain names that match the rules. The following code adds the two fields salary and Quarterlybonus to the employee class:

Added two-Domain 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 Module1 module in the preceding code snippet provides a subroutine's main function, which is where the vb.net program begins. To compile the source program, you must provide a way to access the main sub in one or another way.

If you are not using Visual Studio.NET, you can compile the vb.net source using the vbc.exe software, vbc.exe installed automatically when the. NET framework is installed. For example, when you save the source code as a Employee.vb file, in the directory where Employee.vb is located, enter vbc Employee.vb to compile the source program.

Now let's take a look at the above code, the subroutine's main function first defines a variable ━━anemployee of the employee type:

Dim Anemployee as Employee

Then use the key word new to initialize the employee:

Anemployee = New Employee ()

So we get a variable of the employee type, and we can use its functionality (the Ford Motor Company's engineer produces the car and we can start and drive it.) )。 In our example, the Printsalary method can be invoked using the following method:

Anemployee.printsalary ()

This method prints the value of the salary variable in the employee.

Of course, we can also move the subroutine's main function to the class definition so that we don't have to use the module anymore. The following code demonstrates this method: The subroutine's main function is in the definition of 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: The System.Console.Write in the Printsalary method indicates that we called the Write method in the console class, and the console class is part of the System namespace. The essentials for namespaces will be discussed in the following sections:

Name space

in writing. NET software, we use classes and other types. To make your application more organized, you can combine classes into namespaces, as is the case with Microsoft's. NET Framework class Library. If you open the. NET Framework Class Library in the. NET Framework SDK documentation, you will see that there are more than 80 namespaces, requiring frequent and even significant namespaces including System, System.IO, System.Drawing, System.Windows.Forms and so on. For example, in the Printsalary method of the employee class, we used the console class in the System namespace.

If you want to use namespaces in your program, you can import it first so that you do not have to repeat the name space each time you use its members. For example, you can rewrite the code in table 4, 5 as shown in table 6 below:

Import name space

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

Well, we can now use the console class in the Printsalary method without referencing the namespace, because we've already imported the namespace.

We can also have classes with the same name in different namespaces. To use a class correctly, the usual practice is to use the name space's name before a class. For example, you can use the console class in the System namespace in a system.console manner.

Access type

In many cases, we will provide the written classes to others to use the functionality it provides, for example, they can invoke a method of the class or access one of the fields. One of the great benefits of object-oriented programming is that developers can easily control access to class members, which means that we have complete control over the parts that we want to be used by others.   We can make a method available to other developers, or you can make a class member accessible only in that class. In vb.net, access is graded. Here we discuss these levels:

Ppublic:public class members do not have access restrictions. Adding the public key before a class member allows it to be accessed randomly. 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 the class. You can make a class member secret by using the Private keyword.

Protected: A protected class member can only be accessed by a derived class of that class and within the class itself. You can use the protected keyword to make a class member a protected class member.

Friend: A class member with a friend-level access restriction can only be used within the program that defines the class, and using the Friend keyword makes the class member have a friend-level access restriction.

Protected friend: This is a combination of Protected and friend two types of access. These different access types make object-oriented programming have the ability of information hiding TE. That is, we can use these access types to protect information that is unwilling to be accessed by others.

Static members

Let's take a look at the employee classes in tables 4, 5, and 6, and perhaps the reader will not understand the write that we use it without instantiating the System.Console class, why should we do that? Because in object-oriented programming languages, there is a special class member called static member, VB. NET also has the concept of static members.

You can use static members of an object without instantiating it. For example, in table 7 below, only static fields are included in the SalaryLevel class:

Members of the class

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 a class in a program as shown in 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 ()

' Output payroll to console using static domain of SalaryLevel class

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 this case without first creating the SalaryLevel class variable. A class member that is not part of a static member is called an instance member.

Construction device

The A constructor is a special method that class initialization must use, in vb.net, this method is called new. But we can see in the previous code that we don't have a new method defined in the class. Just so, if the class does not have a constructor defined, VB. NET automatically creates a constructor that invokes the constructor of the class when the object is initialized with the turn off word new. Of course, we can also write the code that the object runs when it initializes.

If we create a constructor in the program, VB. NET will no longer automatically create a constructor for the class.

Inherited

Inheritance is an attribute of an extended class. If you need to complete some functionality, of course you can create a new class, but if someone else creates a class that can provide a part of the functionality you need, you can create a new class that expands the original class, and the class we create can be called a subclass or a derived class, and the original class can be called the underlying class or the parent class. In some cases, subclasses and inheritance are also used to describe extensions to classes. In vb.net, a class can inherit only one parent class, and multiple classes of inheritance are not allowed in vb.net.

Syntactically, a colon after the class name, followed by the key word inherits and the name of the parent class, can be used to complete inheritance of the class. For example, the Code in table 9 below creates a new class called the manager by expanding the employee class:

Extension 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 keyword appears on the next line, the semicolon after the subclass name is not needed, as shown in the following code:

Class Manager

Inherits Employee

End Class

Now we can initialize a Manager object and use the members of the employee. As shown in the Code in table 10 below:

Initializing 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 shows how to extend the manager category by writing a new Printbonus method:

To add a new method to a subclass

Class manager:inherits Employee

Public Sub Printbonus ()

Console.Write (Yearlybonus)

End Sub

End Class

Note the use of member accessibility restrictions. For example, if the Yearlybonus domain has a private property, it cannot be accessed by the manager class, so compiling such code can cause an error.

Inheritance is a common method in object-oriented programming. As a matter of fact. There are many classes in the NETFramework class library that are derived from inheriting 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 ultimately subclasses of the System.Object class, and in the. NET Framework class Library, the System.Object class is called the root or superclass.

The Code in Table 12 illustrates the powerful inheritance features:

Extended System.Windows.Forms.Form

Public Class myform:inherits System.Windows.Forms.Form

End Class

This is an empty class definition that displays a Windows Form when it is compiled and run. Look, we can create a form without writing a single line of code, because MyForm is generated by System.Windows.Forms.Form inheritance, and it inherits the functionality of the Form class.

Classes that are not inheritable

We can use the NotInheritable keyword to make our class not be inherited by others. For example, the calculator in table 13 are not inheritable:

Classes that are not inheritable

NotInheritable Class Calculator

End Class

If you extend this class, you will cause compilation errors. Why does it make our class not inheritable? One reason is that you don't want others to expand our class, and another reason is that an extensible class produces code that runs faster. In spite of this, we should be careful to use classes that are not inheritable, because it does not conform to object-oriented programming, and only if you do not extend this class in 100%, you can make it not inheritable.

In some object-oriented programming languages, these classes are also referred to as final classes.

Conclusion

Vb. NET supports many object-oriented programming features. This article discusses some basic object-oriented features in vb.net, hoping to enable readers to have a basic understanding of object-oriented programming features in vb.net, and to play a useful role.


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.