Level 2 VB Foundation Tutorial:1.3 Basic Concepts of Object-Oriented Programming
1.3 Basic Concepts of Object-Oriented Programming
1. Basic Terminology
(1) Objects and Object Classes
Objects are combinations of code and data, and can be treated as a unit to be processed. An object can be a part of an application, for example it can be a control or a form. The entire application is also an object.
Every object in VB is defined with a class. The relationship between an object and its class is understood by using the analogy between a cookie mold and a cookie. The cookie mold is the class. It defines the characteristics of each cookie, such as size and shape. The class is used to create the object, and the object is the cookie. Classes are the core technology of object-oriented programming and can be understood as a template that defines the behavior and appearance of an object; think of an object as an original copy of a class,
Classes have inheritance, encapsulation, polymorphism, and abstraction.
Attributes are descriptions of the characteristics of the object, VB for each class of objects are specified a number of attributes, the design can change the value of the attributes of specific objects. For example, the background color, height, and width of a form.
(3) events (Event)
Events are actions that occur on an object. Events do not occur randomly, and some events only occur on certain objects.
In VB events are called in the form:
PrivateSub object name_event name
(content of the event)
Method refers to the way to control the behavior of the object action. It is the object itself within the function or process, it is also an action, is a simple do not have to know the details of the event can not be changed, but not called an event; Similarly, the method is not arbitrary, some objects have some specific methods. In VB methods are called in the form:
Object name. Method name
2. Relationship between properties, methods and events
VB objects have properties, methods and events. Properties are data that describes the object; methods tell the object what it should do; events are things that the object produces, and code can be written to handle events when they occur.
VB forms and controls are objects with their own properties, methods, and events. Properties can be thought of as the properties of an object, methods as the actions of an object, and events as the responses of an object.
Objects in everyday life, such as a child’s balloon, also have properties, methods, and events. Properties of a balloon include properties that can be seen, such as its diameter and color. Other properties describe the state of the balloon (inflated or uninflated) or invisible properties such as its lifetime. By definition, all balloons have these properties; these properties can also vary from balloon to balloon.
Balloons also have methods and actions that are inherent to themselves. For example, the method of inflation (the action of filling the balloon with helium), the method of deflation (expelling the gas from the balloon) and the method of ascent (letting go of the balloon and letting it fly away). All balloons have these abilities.
Balloons also have predefined responses to certain external events. For example, a balloon responds to a puncture by deflating it, and to a let go event by lifting off.
In VB program design, the basic design mechanisms are: changing the properties of an object, using the methods of an object, and writing event procedures for object events. The job of program design is to decide which properties should be changed, which methods should be called, and which events should be responded to in order to get the desired appearance and behavior.
3. Event-driven model
In a traditional or “procedural” application, the application itself controls which part of the code is executed and in what order. The program is executed from the first line of code and follows a predetermined path through the application, calling procedures as necessary.
In an event-driven application, code does not follow a predetermined path, but executes different pieces of code in response to different events. Events can be triggered by user actions, by messages from the operating system or other applications, or even by messages from the application itself. The order of these events determines the order in which the code is executed, so the path through the code is different each time the application runs.
Because the sequence of events is unpredictable, certain assumptions must be made in the code about the “various states” of execution. When certain assumptions are made (e.g., assuming that an input field must contain a defined value before a process is run to process it), the application should be structured to ensure that the assumptions are always valid (e.g., disallowing the use of a command button that initiates a process until there is a value in the input field).
Code in execution can also trigger events. For example, changing the text in a text box in a program will raise the Change event for the text box. If the Change event contains code, it will cause that code to be executed. If the original assumption was that the event could only be triggered by user interaction, this could have unintended consequences. For this reason, it is important to understand the event-driven model and keep it in mind when designing applications.
4. Interactive Development
The traditional application development process can be divided into three distinct steps: coding, compiling, and testing the code. But VisualBasic differs from traditional languages in that it uses an interactive approach to developing applications so that there is no longer a clear line between the three steps.
VB interprets code as it is typed, catching and highlighting most syntax or spelling errors on the fly. It looks like an expert is watching the code being typed.
In addition to catching errors on the fly, VB also partially compiles the code as it is typed. When the application is ready to be run and tested, it takes a very short time to compile. If the compiler finds an error, it highlights the error in the code. At that point, you can correct the error and continue compiling without having to start from scratch.
Because of the interactive nature of VB, code can be tested at development time rather than after compilation.
1.4 General Steps in Developing an Application Using VB
A VB program, also known as a project, consists of forms, standard modules, custom controls, and environment settings required for the application. The development steps are generally as follows:
1. Create the user interface of the program
2. Set the properties of each object on the interface
3. Write the program code for the objects to respond to events
4. Save the project
5. Test the application and troubleshoot errors
6. Create the executable program
What Object-Oriented Programming Means
Object-Oriented Programming (OOP) is a programming paradigm whose core idea is to view various things in a program as objects, and to realize the program’s functionality through interactions and collaborations between objects. In Object-Oriented Programming, an object can have its own properties and methods, and can message and interact with other objects to achieve the complex functions of a program.
Object-oriented programming has three basic concepts: encapsulation, inheritance, and polymorphism. Encapsulation refers to encapsulating the data and methods of an object, protecting the object’s private information, hiding implementation details, and improving security and maintainability. Inheritance means that a class can inherit the attributes and methods of another class, avoiding the need to rewrite code and improving code reusability and extensibility. Polymorphism means that objects of the same type can have different behaviors, which achieves flexibility and extensibility for the code.
Object-oriented programming has many advantages, such as modularity, extensibility, reusability, maintainability and readability. The modular nature of object-oriented programming can make the structure of the program clearer and easier to maintain and modify. Meanwhile, the extensibility and reusability of object-oriented programming can make the program have higher reusability, which reduces the development and maintenance cost of the program. In addition, object-oriented programming can also improve the maintainability and readability of programs, making the code easier to understand and modify.
In practice, object-oriented programming is widely used in various programming languages, such as Java, C++, Python and so on. Through the idea of object-oriented programming, programs can be developed more flexibly and efficiently, which improves the reliability and scalability of the program, and also makes the program easier to understand and maintain
A brief description of the basic concepts of object-oriented
In my understanding, object-oriented is a closed extension to the real world model of the self-empty family, which is a kind of “everything is an object” programming ideas. Any object in real life can be categorized as a class of things, and each individual is an instance of a class of things. Object-oriented programming is object-centered and message-driven, so program = object + message.
Object oriented has three main characteristics, encapsulation, inheritance and polymorphism.
Encapsulation is the abstraction of the attributes and behaviors of a class of things into a single class, making the attributes private and the behaviors public, improving the secrecy of the data while making the code modular. This makes the code more reusable.
Inheritance further abstracts the attributes and behaviors common to a class of things into a parent class, and each subclass is a special parent class — having the behaviors and attributes of the parent class, as well as behaviors and attributes unique to itself. Doing so extends already existing blocks of code, further increasing code reusability.
If encapsulation and inheritance are about enabling code reuse, polymorphism is about enabling interface reuse. One of the major roles of polymorphism is for decoupling – for decoupling the parent-child class inheritance. If the relationship between parent and child classes in inheritance is IS-A, then the relationship between interfaces and implementation classes is HAS-A. Simply put, polymorphism is to allow the parent to point to a subclass (or interface) object. Many design patterns are based on object-oriented polymorphic spike spring design.
To summarize, if encapsulation and inheritance is the basis of object-oriented, then polymorphism is the most essential theory of object-oriented. To master polymorphism must first understand the interface, only a full understanding of the interface to better apply polymorphism.
What Object-Oriented Means
About what object-oriented means is as follows:
Object-oriented is a programming paradigm and way of thinking that achieves the design and implementation of a system by abstracting real-world entities into objects, encapsulating the attributes and behaviors of the objects, and achieving the design and implementation of the system through interactions between the objects. The following is a detailed description of object orientation:
1. Objects and Classes
The core of object-oriented programming is the concept of objects and classes. Objects are real-world entities with their own state (properties) and behavioral methods that allow interaction and communication. Classes are a template or blueprint for defining the types and properties of objects. Multiple objects can be created through classes, each with the same properties and behavior.
Encapsulation is one of the important features of object-oriented programming, which encapsulates the properties and behavior of an object, hides internal implementation details, and provides access to the outside world through interfaces. Encapsulation protects the integrity and security of data while providing flexible interfaces for external use.
Inheritance is another important feature of object-oriented programming that allows the creation of new classes and the inheritance of attributes and behavior from existing classes. Through inheritance, subclasses have access to the attributes and methods of the parent class and can be modified, extended, or overridden. Inheritance allows for reuse and hierarchical design of code and improves maintainability and reusability of code.
Polymorphism is one of the important features of object-oriented programming, which allows the same method to produce different behavior on different objects. Through polymorphism, the flexibility and extensibility of the code can be improved, allowing the program to adapt and change according to the actual situation. Proficiency in the concepts and techniques of object-oriented programming is important for improving code quality, development efficiency, and software architecture design.
The mindset of object-oriented programming emphasizes modularity, reusability, and maintainability. It makes the design, implementation and maintenance of programs clearer and more flexible through features such as abstraction, encapsulation, inheritance and polymorphism. Object-oriented programming is widely used in various fields of software development, including application programs, game development, website development, and so on.
How to understand the classes and objects in object-oriented programming
Basic concepts of object-oriented programming: object-oriented, in a nutshell, is to abstract the problem as an object, set the attributes (data), specify the events or processing (code) to achieve the purpose, emphasizing the concept of the domain of the problem to the software program and the interface of the direct mapping. Concepts in object-oriented programming include: objects, classes, data abstraction, inheritance, dynamic binding, data encapsulation, polymorphism, message passing. Through these concepts object-oriented ideas are concretely embodied.
Basic Characteristics of Object-Oriented Programming1) Objects: An object is the basic runtime entity, it is a logical entity that encapsulates data and the code that manipulates that data.2) Classes: A class is an abstraction of an object that has the same type. All the data and code contained in an object can be constructed through a class.3) Encapsulation: encapsulation is an information hiding technique that is embodied in the description of a class and is an important characteristic of an object. Encapsulation so that the data and the processing of the data method (function) encapsulated as a whole, in order to achieve a strong independence of the module, so that the user can only see the external characteristics of the object (the object can receive which messages, with those processing capabilities), while the internal characteristics of the object (save the internal state of the private data and the realization of the processing capabilities of the algorithm) is hidden from the user. The purpose of encapsulation is to separate the designer of the object and the use of the object, the user does not have to know the details of the implementation of the behavior, only the messages provided by the designer to access the object. 4) Inheritance: Inheritance is the mechanism by which subclasses automatically share data and methods between parent classes. It is embodied by the derived functionality of the class. A class directly inherits the entire description of other classes and can be modified and expanded at the same time. Inheritance is transitive. Inheritance is categorized into single inheritance (a subclass has only one parent) and multiple inheritance (a class has more than one parent). The objects of a class are closed, and without an inheritance mechanism, there is a lot of duplication of data and methods in the objects of the class. Inheritance not only supports the reusability of the system, but also promotes the expandability of the system.5) Polymorphism: Objects act according to the messages they receive. The same message received by different objects can produce completely different actions, this phenomenon is called polymorphism. The use of polymorphism users can send a generic message, and all the implementation details are left to the object receiving the message to decide, such as, the same message can call different methods. For example, a Print message sent to a graph or table will invoke a completely different print method than the same Print message sent to a body file. The implementation of polymorphism is supported by inheritance, which utilizes the hierarchical relationship of class inheritance to store protocols with generic functionality as high up in the class hierarchy as possible, while placing the different methods that implement this functionality at lower levels, so that objects generated at these lower levels can respond differently to the generic message. Polymorphism can be achieved in OOPL by redefining base class functions (defined as overloaded or dummy functions) in derived classes.6) Dynamic binding
Binding refers to the act of linking a procedure call to the corresponding code. Dynamic binding refers to the code associated with a given procedure call is only known at runtime a binding, it is a specific form of polymorphic implementation.7) message passing: objects need to communicate with each other, the way to communicate is to send and receive messages between objects. The content of the message includes the identity of the object that receives the message, the identity of the function that needs to be called, and the necessary information. The concept of message passing makes it easier to describe the real world.
For a language to be called object-oriented it must support several major object-oriented concepts. Depending on the level of support, what is commonly referred to as an object-oriented language can be divided into two categories: object-based languages, and object-oriented languages.
Object-based languages support only classes and objects, while object-oriented languages support concepts such as classes and objects, inheritance, and polymorphism. For example, Ada is a typical object-based language because it does not support inheritance, polymorphism, in addition to other object-based languages such as Alphard, CLU, Euclid, Mola. part of the object-oriented language is a newly invented language, such as Smalltalk, Java, these languages themselves tend to absorb the essence of other languages, while trying to Some of the object-oriented languages are newly invented languages, such as Smalltalk and Java, which tend to absorb the essence of other languages and try to eliminate their deficiencies, so the object-oriented features are especially obvious and full of vigor; others are the evolution of existing languages that have been modified to increase the object-oriented features.
What is the concept of class in object-oriented programming language
1, class (class), as the father of the code, it can be said that it wraps a lot of interesting functions and methods, as well as variables, class methods static methods, class methods, ordinary methods class is generally commonly used there are three kinds of methods, that is to say, for staticmethod (static method), classmethod (class method) and self (ordinary method). method), classmethod (class method) and self (common method).
2, a collection of common attributes is called a class, such as the collection of people, with gender, age, date of birth and other attributes; if the class has a common skill such as the skill of speaking, this skill is called a common method.
3. What is a class: an abstraction of objects with the same or similar properties is a class. Classes have attributes, it is an abstraction of the state of the object, with a data structure to describe the attributes of the class. Classes have operations, which is an abstraction of the behavior of an object, described by the operation name and the method that implements the operation.
4, object-oriented programming in the concepts include: objects, classes, data abstraction, inheritance, dynamic binding, data encapsulation, polymorphism, message passing. Through these concepts object-oriented thinking has been specifically embodied.
5, in the object-oriented programming language, class is a class of “things” of the properties and behavior of the abstraction.
6, class and object are the most basic concepts in object-oriented programming techniques. Classes are abstractions of objects, and objects are concrete instances of classes. Classes are abstract and do not occupy memory, while objects are concrete and occupy storage space.