Frog Frog Recommended: Basics of Object-oriented Programming (vb.net edition)

Source: Internet
Author: User
Tags inheritance
All operations that are performed almost in Visual Basic are associated with objects. If you first contact object-oriented programming, the following terms and concepts will help you get started.
Classes and objects
The word "class" and "object" are used so much in object-oriented programming that it is easy to confuse them. Generally speaking, "class" is an abstract representation of some content, while "object" is an example of what the class represents. Shared class members are an exception to this rule, which can be used in instances of a class and in object variables declared as shared class types.
Fields, properties, methods, and events
A class consists of fields, properties, methods, and events. Fields and properties represent the information that an object contains. fields are similar to variables because they can be read or set directly. For example, if you have an object named car, you can store its color in a field named color.
Properties are retrieved and set in a similar way to fields, but properties are implemented by using the Property Get and attribute set procedures, which provide more control over how to set or return values. The indirect layer between the stored value and the procedure that uses this value helps isolate the data and allows you to validate the values before assigning or retrieving them.
Method represents an action that an object can perform. For example, a car object can have StartEngine, Drive, and Stop methods. You define a method by adding a procedure (a Sub routine or function) to the class.
An event is a notification that an object receives from another object or application, or a notification that an object is transferred to another object or application. Events allow an object to perform an action whenever a particular situation occurs. An example of an event for the car class is the Check_engine event. Because Microsoft Windows is an event-driven operating system, events can come from other objects, applications, or user input (such as mouse clicks or keystrokes).
Encapsulation, inheritance, and polymorphism
Fields, properties, methods, and events are only half the content of object-oriented programming. Real object-oriented programming requires objects to support three of features: encapsulation, inheritance, and polymorphism.
Encapsulation means that a set of related properties, methods, and other members are treated as a unit or object. Objects can control how properties are changed and how methods are executed. For example, an object can validate a value before allowing the property to change. By hiding the implementation details of an object, a practice called data hiding, encapsulation also makes it easier to make changes to the implementation at a later time.
Inheritance describes the ability to create new classes based on existing classes. New classes inherit all of the properties, methods, and events of the base class, and you can customize the new class with other properties and methods. For example, you can create a new class named truck based on the car class. The truck class inherits the Color property from the car class and can have other properties, such as fourwheeldrive.
Polymorphism means that you can have multiple classes that can be used interchangeably, even if each class implements the same property or method in different ways. Polymorphism is the essence of object-oriented programming because it allows the use of an item of the same name, regardless of what type of object is being used at this time. For example, assuming a given base class car, polymorphism enables programmers to define different startengine methods for any number of derived classes. The StartEngine method for a derived class named Dieselcar can be completely different from a method with the same name in the base class. Other procedures or methods can use the StartEngine method of a derived class in exactly the same way, regardless of what type of car object is used at this time.

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.