Object-oriented programming - VB.NET & C # articles

zhaozj2021-02-16  53

Object-oriented programming

--VB.NET & C # articles

introduction

.NET is a language platform that fully reflects object-oriented (oo) features, including:

Namespace

Classes

Objects

Encapsulation

Overloading

Inheritance

Overriding

Interface (Interfaces)

Polymorphism

Let's take a brief sense to understand these features one by one:

Namespace

Even if the name is widely used, it is not a real OOPS feature, it is just a logical container of a variety of different classes. The class name under the name space must be unique, both of the names have two purposes - providing logical organizations and avoiding ambiguity for classes.

Classes

Class, just a definition of "template" or "blue-print". For example, you can define a true existence class -employee, which contains property members (Name, Age ...) and behavior (Calculate, Goonleave ...).

Objects

The instance of the class is called object. For example, the EMPLOYEE class mentioned above can include three instances, which depicts specific employees - John, Bob and Tom.

Encapsulation

Each object (Objects) is representatively assigned some data or anything else. Not all data is exposed to the outside of the system, which can be controlled by packaging.

Overloading

Overloading means that the method or function can use the same name, but use different parameter names. The data type and number of parameters can be changed.

Inheritance

Inheritance refers to the extension of the class. It plays an important role in the development of your system in any time. The .NET program only supports a single inheritance relationship.

Overriding

Rewriting means that the method is used in the derived class with the name of the parent class (except for parameters).

Interface (Interfaces)

The interface is only the model of the properties and methods of classes that do not need to perform any code. The class uses the import keyword to perform the interface. When a class creates an interface through the Implements keyword, it must perform all properties and methods (although this action is empty or invalid).

Polymorphism

Multi-mode means that the system uses the same name to call the correct task.

Create a namespace

Create a namespace Use Keywords --NameSpace (using namespace in C #). The following example shows how to declare a namespace.

[Vb.net]

Namespace mynamespace

...

End Namespace

[C #]

Namespace mynamespace

{

...

}

Note that a collection can have one or more namespaces. Instead, a namespace can span multiple collections. You can create a namespace nest like the following example.

[Vb.net]

Namespace mynamespace

Namespace Musubnamespace ...

End Namespace

End Namespace

[C #]

Namespace mynamespace

{

Namespace mysubnamespace

{

...

}

}

If you are using the VS.NET integration development environment to create a solution, use the default namespace.

Create class

Class creation is similar to the creation of the name.

[Vb.net]

Public Class Class1

...

END CLASS

[C #]

Public Class Class1

{

...

}

Class is usually part of some namespaces.

Create properties

Attribute is a class package data member, attribute can be read or written, read and write only. This is an example of creating a "read and write" attribute:

[Vb.net]

Public Class Employee

Private strname as string

Public Property Name As String

Get

Return Strname;

END GET

Set (Value As String)

Strname = value;

End set

End Property

END CLASS

[C #]

Public Class Class1

{

Public String Name

{

String strname;

get

{

Return Strname;

}

set

{

Strname = value;

}

}

}

Note: 1. VB.NET uses the Property keyword declaration property, C # uses this keyword.

2. Property definitions consists of both part of GET and SET, and the value of the GET section returns the attribute and the SET section is to set some Private variables.

3, the value of Set settings is called this value by implies variables in C #, and you allow you to change this value in VB.NET.

Creation method

Method depicts the behavior of the object. In VB.NET's Function and SUB blocks, you can imagine the matrix call, anything in C # is a function.

[Vb.net]

Public Sub Calculatesalary ()

...

End Sub

[C #]

Public void cagculatesalary ()

{

...

}

Create an overload

Method Overloading refers to the order of the different data types and their order. The following example clear demo method is overloaded:

[Vb.net]

Public Sub Calculatesalary ()

...

End Sub

Public Sub CalculateSalary (Month As Integer)

...

End Sub

[C #]

Public void cagculatesalary ()

{

...

}

Public void CalculateSalary (int MONTH)

{

...

}

Note: You can also use any parameters to complete similar features with any parameters, however, Microsoft recommends the use of overloaded methods.

inherit

Inheritance is a capability that extends existing classes. .NET allows a single inheritance not like C to allow inheritance, which means that you can only inherit from a single class at any moment.

[Vb.net]

Public Class Manager

Inherits EMPLOYEE

...

END CLASS

[C #]

Public Class Manager: Employee

{

...

}

In the above example, we have established a derived manager inherited from the parent class Employee like you can guess the specific implementation of Manager is the specific implementation of the parent Employee. VB.NET uses the inherits keyword to represent the parent class and use the ":" operator in the C # code.

Method rewriting

In order to override the method in the subclass, you must identify the Overridable key (VB.NET) Virtual keyword (C #) before the method in the parent class. [Vb.net]

Public overridable function calculatesalary () AS Integer

...

END FUNCTION

[C #]

Public Virtual Int CalculateSalary ()

{

...

}

Then, in subclasses you can create a method using the same name and the base class method that specifies the rewritten.

[Vb.net]

Public overrides function CalculateSalary () AS Integer

...

END FUNCTION

[C #]

Public override int CalculateSalary ()

{

...

}

Note: If you do not provide keyword overrides (vb.net) or Override in the subclass, the compiler gives a warning tips for "You Are Hiding A Bases Member". Of course, you can also enter shadows (vb.net) or new (C #) keyword to hide the base class member.

Create an interface

The like is the same as the template, the interface can be a class template, the same ground, they are in the object model.

[Vb.net]

Public Interface IemPloyee

Property Employeei () AS Integer

Property name () AS STRING

Property agn () AS Integer

Function Calculatesalary () AS Integer

End interface

[C #]

Public Interface IemPloyee

{

INT Employeeid

{

Get;

}

String name

{

Get;

SET;

}

Int ag

{

Get;

SET;

}

INT Calculatesalary ();

}

Polymorphism

The following paragraph as an example of explanation:

[Vb.net]

DIM EMP As Employee

EMP = New Clerk ()

Console.writeLine

("Clerk Salary: {0}", Emp.calculate ())

EMP = new manager ()

Console.writeLine

("Manager Salary: {0}", Emp.calculatesalary ())

[C #]

Employee EMP;

EMP = New Clerk ();

Console.writeLine

("Clerk Salary: {0}", Emp.calculatesarary ());

EMP = new manager ();

Console.writeLine

("Manager Salary: {0}", Emp.calculate ());

In the example, we defined a variable of an Employee type. The variable of the parent class type can point to an example of any child. First, let's let it point to an instance called the Clerk class, then let's let it point to an instance called the Manager class. Rely on subtype, parent type (here Type Employee) can even correct CalculateSalary () methods. The above action can be done by Inheritance Polymorphism and Interface Polymorphism.

[Vb.net]

DIM EMP As IEmployeeemp = New Clerk ()

Console.writeLine

("Clerk Salary: {0}", Emp.calculate ())

EMP = new manager ()

Console.writeLine

("Manager Salary: {0}", Emp.calculatesalary ())

[C #]

Iemployee EMP;

EMP = New Clerk ();

Console.writeLine

("Clerk Salary: {0}", Emp.calculatesarary ());

EMP = new manager ();

Console.writeLine

("Manager Salary: {0}", Emp.calculate ());

to sum up:

Through this article we can understand many of the VB.NET and C # OO (object-oriented) features, in the field of VB.NET and C # language programming, you must have an object-oriented language characteristics and programming ideas for using keywords.

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

New Post(0)