Introduction to object oriented technology based on Oracle developer network Oracle
- 2020-06-01 11:13:10
1. An overview of the
Object is a new feature in Oracle8i. Object is actually a encapsulation of a group of data and operations. The abstraction of object is a class. In object-oriented technology, objects involve the following important features:
Through the encapsulation of data and operations, the data and operations that users care about are exposed as interfaces, while the other data and operations are hidden inside the object, which is easy for users to use and maintain.
Object inheritance, through this 1 can enhance the program's extensibility, suitable for the development of large projects.
If the same 1 operation has a different object to reference at run time, the result will be different. This 1 property is called polymorphism.
Because of the many advantages of being object-oriented, Oracle has added full support for this feature since version 8.0. The following sections focus on object-oriented programming in Oracle.
2. Object-oriented programming in Oracle
Since objects have so many advantages, how do you reference them in the Oracle database? The object definition in Oracle takes place in two steps:
First, define the object type. Defining the object type is exactly the same as defining the package type, which is divided into the object type header (or object specification, specification) and the object type body (body). The object type header contains the declaration of the properties and methods of the object type, while the object type body contains the concrete implementation of the object type.
For example, define an empObj object type with the following code:
In particular, if an object does not have a member function part, the object type is defined only in the header part of the object type.
The object instance is then defined. After defining the object type, you can directly define its instance, such as defining an empObj instance object. The code is as follows:
After these two steps, you can refer to the properties and methods of the object instance with the reference symbol ". ", for example
In addition, when initializing an object without a member function, you can do so in the form of a constructor. Note that you do not need to explicitly define a constructor at this point. For example, initialize the above v_empObj1 object (assuming no member functions) with the following code:
Since the Oracle database is a relational database, the data is stored in the form of a 2-dimensional table, while the object is an entity that encapsulates data and operations. The information stored in the Oracle database is usually multi-dimensional. So how is the object stored in the Oracle database? (to add 1 point here, the object declared in the PL/SQL block is a temporary object, and the system will automatically retrieve its allocated resource after it has exceeded its scope, but if the information of the object needs to be saved, it must be stored in the database.)
In fact, objects are stored in the Oracle database in two ways:
1. Object. That is, you can define the data type of a column in a data table as an object type so that the object can be stored in a data column. For example, define a table, table1, where the emp column can be used to store objects.
2. Object. That is, one object table can be created, in which each column represents one property in the object, so that one row record is one object. For example, define 1 emp table as follows:
In this way, one record of emp table is one empObj object, and the operation of inserting one table can be as follows:
Note that here the column type in the table should correspond to the property type of the object 11, and that storing this information will ignore the member functions of the object.
3. Object operation and comparison
You can use the DML statement to operate on the object. The syntax of the operation is exactly the same as that of the 1-like data type. For example, in the table1 table, you can return the record of the object empObj(10002,'mike',3000) :
If you need to compare the size of an object, it's hard to do it in a 1-like way, because an object has a set of properties and cannot be compared in combination. This problem can be solved by adding the map method and order method to the object. The former is to compare the size of an object by returning a property of an object to the watch representing the object, and the latter is to get the size of an object by comparing the watch of a property between two objects. Due to the similarity between the two, the more widely used map member function is illustrated as follows:
After the map function is defined in this way, the comparison of the size of empObj object is essentially transformed into the comparison of the size of emp_id attribute of each object. In the actual operation, the concerned data should be returned according to the actual situation, so as to perform the operation of object size comparison.
Through the introduction of the previous content, you should have a preliminary understanding of the object-oriented characteristics of Oracle database. By making full use of the characteristics of Oracle, you can introduce the advantages of object-oriented reuse and extensibility into the database and improve the performance of the database.
On 1 page