In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Object-oriented programming, referred to as OOP programming, is actually an abstract process of things in the real world. Its core is to distinguish between the definition and implementation of the object, to define the structure of part of the object, and to implement the part according to the specific structure defined in the definition part.
The mold used to produce toys is called class, and the process of designing class can also be called modeling. Of course, this mold is not the mold of the mold, but the model of the class. The toy produced can be called an object, the class is the abstraction of the object, and the object is the concrete instance of the class. Classes are abstract and do not take up memory, while objects are concrete and take up storage space. A class is a blueprint for creating objects, and it is a software template that defines methods and variables included in a particular type of object.
The definition of a class is a collection of objects with the same data structure and the same operation, and the definition of a class includes a set of properties that represent its properties. And a set of methods that represent its execution behavior, the class definition can be regarded as an object template with similar characteristics and common behavior, and can be used to generate objects, and each object is an instance of the class, and the methods provided in the class can be used. The specific state of the object is contained in the object instance variable.
Encapsulation: also known as information encapsulation, ensures that objects do not change the internal state of other objects in unexpected ways. Other internal states can be accessed only in objects that provide methods for changing internal states. Each type of object provides an interface to contact other objects and specifies the methods to be called by other objects.
Polymorphism: references and classes of objects involve many other different types of objects, and the results produced by referenced objects will depend on the type actually invoked
Inheritance: allows the creation of subclass objects based on existing objects, unifying and enhancing polymorphism and encapsulation. Typically, objects are grouped by classes, and new classes can be defined as extensions of existing classes, so that classes can be organized into a tree or mesh structure, which reflects the generality of objects.
The object type is actually the type introduced in the previous section, and the instance of the class is the object. Object types encapsulate data structures and the processes and functions used to manipulate these data structures, which makes it possible to encapsulate some complex code by defining object types, thus improving the efficiency and speed of application development. You can declare properties and methods in an object type description section. Properties cannot be constants, exceptions, cursors, or types. The maximum number of property declarations is 1000, but at least one property must be declared, and the method is optional. The property describes the properties of the object, and the method is the function of the object type. In PLSQL, a method is a subroutine that can be a function or a procedure. The method name cannot be the same as its object type name and property name, and the method is called at the instance level or the object type level.
The composition and structure of objects in PLSQL
The object type in PLSQL is a custom conformance type, and its definition is very similar to that of the package.
Object type specification: an interface between an object and an application, which is used to define common properties and methods of an object
Object type body: used to implement common methods defined by the object type specification.
For example, when defining the employee object type, we first define all the properties of the object in the object type specification, as well as the method declaration that the object can be called. These methods have no specific implementation part and can only be signed by externally called methods. The specific method body code implementation is defined in the object type body.
When defining properties of an object type, you cannot specify default values for object properties, nor can you specify the NOT NULL option.
Several types of methods that can be defined in PLSQL
1. Constructor: this method is similar to a constructor in languages such as JAVA, and is used to initialize an object type and return an instance of the object.
2.MEMBER method: this method allows an instance of an object to be called, and the data of an object instance can be accessed in the MEMBER method, usually called an instance method or a member method
3.STATIC method: this method can be called directly on the object type and is used to perform global operations on the object type, often referred to as static methods
4.MAP method: a mapping method used to sort among multiple objects.
5.ORDER method: the sort method used to sort between two object instances.
Define object types
The object type includes two parts: the object type specification and the object type body, so the object type specification must be defined first, and then the object type body must be defined.
Define the employee_obj object specification
Create or replace type employee_obj as object (
Empno number (4)
Ename varchar2 (20)
Job varchar2 (20)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
MEMBER procedure change_sal (p_empno number,p_sal number)
MEMBER procedure change_comm (p_empno number,p_comm number)
MEMBER procedure change_deptno (p_empno number,p_deptno number)
MEMBER procedure get_sal (p_empno number) return number
MEMBER procedure get_comm (p_empno number) return number
MEMBER procedure get_deptno (p_empno number) return integer
) NOT FINAL;-specifies that the class can be inherited. If FINAL is specified, it means that the class cannot be inherited.
Define the object
Create or replace type body employee_obj
As
Member procedure chang_sal (p_empno number,p_sal number)
Is
Begin
Update emp set sal=p_sal where empno=p_empno
End
Member procedure chang_comm (p_empno number,p_sal number)
Is
Begin
Update emp set comm=p_comm where empno=p_empno
End
Member procedure chang_deptno (p_empno number,p_sal number)
Is
Begin
Update emp set deptno=p_deptno where empno=p_empno
End
Member function get_sal (p_empno number)
Return number
Is
V_sal number (10Phone2)
Begin
Select sal into v_sal from emp where empno=p_empno
Return v_sal
End
Member function get_comm (p_empno number)
Return number
Is
V_comm number (10Phone2)
Begin
Select comm into v_comm from emp where empno=p_empno
RETURN v_comm
End
Member function get_deptno (p_empno number)
Return integer
Is
V_deptno int
Begin
Select deptno into v_deptno from emp where empno=p_empno
Return v_deptno
End
End
Define attribute
An attribute is the definition of an object type property, and an attribute declaration is required for an object, that is, an object type must define at least one property. The definition of an attribute is similar to that of a variable, and it also has a name and data type. The name of the attribute must be unique throughout the object type, but the naming of the attribute can be repeated between different object types.
1. The declaration of the property must precede the declaration of the method, that is, in the object specification, the declaration under create type must first be the definition of the property.
two。 The data type of the property must be an oracle database type, not any PLSQL type or PLSQL custom type, but the rowid,urowid,long,log raw,nchar,nclob,nvarchar2 type in oracle is excluded.
3. You cannot apply NOT NULL constraints to attributes or use default to specify default values when defining attributes.
4. At least one property must be defined in an object type, but not more than 1000 attributes.
The type of an attribute can be either a simple data type or a reference to an object type.
Define the properties of an object
Create or replace type employee as object (
Empno number (4)
Ename varchar2 (20)
Job varchar2 (20)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
) NOT FINAL
After defining the object type, you can read or write the attribute values of the object in the PLSQL statement block by instantiating the object type.
Declare
V_emp employee_property
V_sal v_emp.sal%TYPE
Begin
V_emp:=employee_property ('Zhao Wu', 'salesperson', 5000pr. 200j020)
V_sal:=v_emp.sal
Dbms_output.put_line (v_emp.ename | | 'salary is:' | | v_sal)
End
Definition method
Object methods are subroutines that are declared in the object description section using MEMBER or STATIC in the object specification definition, which are done after the property declaration
MEMBER method: member methods are called based on the object instance rather than the object type
STATIC method: static methods are independent of the object instance and cannot reference the object's properties in the object type body
Using MEMBER and STATIC member methods
Create or replace type employee_method as object (
Empno number (4)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
MEMBER procedure change_sal,-- instance method, which can access the properties of the object itself
MEMBER function get_sal return number
Static methods that cannot access the properties of the object itself, but can only access static data
STATIC procedure change_deptno (p_empno number,p_deptno number)
STATIC function get_sal (p_empno number) return number
) NOT FINAL;-specifies that the class can be inherited. If FINAL is specified, it means that the class cannot be inherited.
Create or replace type body employee_method
As
MEMBER procedure change_sal
Is
Begin
Self.sal:=self.sal*1.12
End
MEMBER function get_sal
Return number
Is
Begin
Return sal
End
STATIC procedure change_deptno (p_empno number,p_deptno number)
Is
Begin
Update emp set deptno=p_deptno where empno=p_empno
End
STATIC function get_sal (p_empno number)
Return number
Is
V_sal number (10Phone2)
Begin
Select sal into v_sal from emp where empno=p_empno
Return v_sal
End
End
Examples of using MEMBER and STATIC methods
Declare
V_emp employee_method
Begin
V_emp:=employee_method (7999, 5000, 200, 20);-- instantiate the employee_method object, and now v_emp is the object instance
The object instance method, that is, the MEMBER method
Dbms_output.put_line ('employee ID:' | | v_emp.empno | | 'salary is:' | | v_emp.get_sal)
The following code calls the STATIC method to update the department number 20 in the emp table with employee number 7369.
Employee_method.change_deptno (7369 and 20)
The following code gets the salary of employee number 7369 in the emp table
Dbms_output.put_line ('salary for employee number 7369 is:' | | employee_method.get_sal (7369))
End
Use the SELF keyword
Each MEMBER type method implicitly declares an inline parameter SELF, which represents an instance of the object type and is always passed to the first parameter of the member method. In fact, the method body can also be used without SELF.
Access the properties of an object type
Create or replace type employee_salobj as object (
Empno number (4)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
MEMBER procedure change_sal
MEMBER procedure change_comm
MEMBER procedure change_deptno
MEMBER function get_sal return number
MEMBER function get_comm return number
MEMBER function get_deptno return INTEGER
) NOT FINAL
Create or replace type body employee_salobj
As
MEMBER procedure change_sal
Is
Begin
Self.sal:=self.sal*1.12
End
MEMBER procedure change_comm
Is
Begin
Comm:=comm * 1.12
End
MEMBER procedure change_deptno
Is
Begin
Self.deptno:=20
End
MEMBER function get_sal
Return number
Is
Begin
Return sal
End
MEMBER function get_comm
Return self.comm
End
MEMBER function get_deptno
Return integer
Is
Begin
Return self.deptno
End
End
In the member implementation of the employee_salobj object type body, self is explicitly used for access to object properties, which is actually implicitly used when the self keyword is not explicitly used.
Because STATIC belongs to the static method level, it cannot accept or reference the self keyword
Define constructor
When an object type is defined, the system provides a constructor that receives the parameters corresponding to each property.
Constructors are generally defined for the following purposes:
1. Provide initialization functions for objects to prevent many special-purpose processes from initializing only different parts of the object, which can be initialized uniformly through constructors
two。 You can provide default values for certain properties in the constructor, which ensures that the property values are correct without having to rely on every property value provided by the caller.
3. For maintainability, avoid changing the code in the application that calls the constructor when new properties are added to the object, so that existing constructor calls can continue to work.
The constructor is defined as a function with the same name as the object type name that initializes the object and returns a new instance of the object type. When you customize the constructor, you either override the default constructor generated by oracle for each object, or define a new constructor with a different method signature. Custom constructors are declared using the constructor keyword.
Custom constructor example
Create or replace type salary_obj as object (
Percent number (10d4),-- define object types
Sal number (10Phone2)
-- Custom constructor
Constructor function salary_obj (p_sal number) return self as result)
Instantiable
Final
/
-- define the object type body
Create or replace type body salary_obj
As
-- A constructor that implements overloading
Constructor function salary_obj (p_sal number)
Return self as result
As
Begin
Self.sal:=p_sal
Self.percent:=1.12
Return
End
End
/
Call
Declare
V_salobj1 salary_obj
V_salobj2 salary_obj
Begin
V_salobj1:=salary_obj (1.123000)-- use the default constructor
V_salobj2:=salary_obj (2000);-- using custom constructors
End
Define MAP and ORDER methods
MAP method: this function returns the object instance to the scalar type of FATE,NUMBER,varchar2 according to certain calling rules. After the mapping object type is a scalar function, the result can be obtained by comparing the scalar function.
ORDER method: the order method can only compare two objects. It must be a function that returns a numeric result, either positive, negative, or zero based on the result. The method takes only two parameters, SELF and another object type to compare, and returns NULL if the parameter is passed as NULL.
Because the MAP method maps all objects to a single scalar value when called in one call, it is commonly used when sorting or merging many objects. On the other hand, the order method can only compare two objects at a time, so it is less efficient to be called repeatedly when comparing multiple objects.
Example of defining a MAP function
Create or replace type employee_map as object (
Empno number (4)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
MAP MEMBER FUNCTION convert return real-defines a MAP method
) NOT FINAL
Create or replace type body employee_emp as
MAP MEMBER FUNCTION convert return real is-defines a MAP method
Begin
Return sal+comm
End
End
After the MAP function is defined, PLSQL implicitly sorts or compares multiple objects by calling the MAP function. For example, the following creates an object table for emp_map_tab, inserts multiple objects into the object table, and then sorts the objects on the object table
Create table emp_map_tab of employee_map
Insert into emp_map_tab values (7123, 3000, 200, 20)
Col val format a60
Select value (r) val,r.sal+r.comm from emp_emp_tab r order by 1
Example of defining an order function
Create or replace type employee_order as object (
Empno number (4)
Sal number (10Phone2)
Comm number (10Phone2)
Deptno number (4)
ORDER MEMBER FUNCTION match (r employee_order) return integer-defines an ORDER method
) NOT FINAL
Create or replace type body employee_order as
ORDER MEMBER FUNCTION match (r employee_order) return integer is
Begin
If ((SELF.sal+SELF.comm) (r.sal+r.comm)) then
Return 1
Else
Return 0
End if
End match
End
Once the order function is defined, you can compare the two objects.
Declare
Emp1 employee_order:=employee_order (7112, 3000, 200, 20)
Emp2 employee_order:=employee_order (7113, 3800, 100, 20)
Begin
If emp1 > emp2 then
Dbms_output.put_line ('salary plus commission for employee 1 is bigger than employee 2')
Elsif emp1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.