создание объекта транспортного средства и подтипов под ним (механический автомобиль и велосипед).Под механическим транспортным средством установлен грузовой автомобиль.У велосипеда есть процедура-член set_gear_count set_gear_count принимает два параметра: счетчик передач и идентификатор автомобиля и обновляет счет передач велосипеда.Я должен продемонстрировать, как реализовать и вызвать метод и показать результат операции.
Я попытался реализовать типы и метод, как показано ниже. Но я имею на велосипеде процедуру set_gear_count.функция обновления не работает
CREATE OR REPLACE TYPE vehicle AS OBJECT(
vehicle_id NUMBER,
manufacturer VARCHAR2(20),
purchase_date Date,
color varchar2(20),
MAP MEMBER FUNCTION getvidno RETURN NUMBER,
MEMBER FUNCTION get_vehicle RETURN varchar2)NOT FINAL;
/
CREATE OR REPLACE TYPE BODY vehicle AS
MAP MEMBER FUNCTION getvidno RETURN NUMBER IS
BEGIN
RETURN vehicle_id;
END;
MEMBER FUNCTION get_vehicle RETURN VARCHAR2 IS
BEGIN
RETURN 'Id: ' || TO_CHAR(vehicle_id) || ' Manufacturer: ' ||
manufacturer || ' purchase_date: '|| TO_CHAR(purchase_date) || '
Color: ' || color;
END ;
END;
/
desc vehicle;
/
CREATE OR REPLACE TYPE powered_vehicle UNDER vehicle(
fuel_type VARCHAR2(20),
license_number VARCHAR2(10),
model VARCHAR2(10),
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2 )NOT FINAL;
/
CREATE OR REPLACE TYPE BODY powered_vehicle As
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2 IS
BEGIN
RETURN (self AS vehicle).get_vehicle || ' Fuel_type: ' || fuel_type || ' license_number: ' || license_number ||' model:'|| model;
END ;
END;
/
desc powered_vehicle;
CREATE OR REPLACE TYPE truck UNDER powered_vehicle(
cargo_capacity NUMBER,
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2
) FINAL;
/
CREATE TYPE BODY truck As
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2 IS
BEGIN
RETURN (self AS powered_vehicle).get_vehicle || ' cargo_capacity: ' || cargo_capacity;
END ;
END;
/
desc truck;
CREATE TABLE Vehicles_tab (v_id varchar2 (255) AS (CONCAT (vehics.vehicle_id, TO_CHAR (CONCAT ('-', vehics.purchase_date))))) НЕ НУЛЬ, vehicsтранспортное средство);
ALTER TABLE vehicles_tab ADD(
CONSTRAINT vehx_pk PRIMARY KEY(v_id));
CREATE SEQUENCE vehic_seqx START WITH 1;
CREATE OR REPLACE TRIGGER vehic_birx
BEFORE INSERT ON vehicles_tab
FOR EACH ROW
BEGIN
SELECT vehic_seqx.NEXTVAL
INTO :new.vehics.vehicle_id
FROM DUAL;
END;
/
CREATE OR REPLACE TYPE bicycle UNDER vehicle (
gear_count NUMBER,
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2 ) FINAL;
/
CREATE OR REPLACE TYPE BODY bicycle as
OVERRIDING MEMBER FUNCTION get_vehicle RETURN VARCHAR2 IS
BEGIN
RETURN (self AS vehicle).get_vehicle || ' Gear count: ' ||TO_CHAR(gear_count) ;
END get_vehicle;
create or replace type body bicylce as
member PROCEDURE set_gear_count(gearcnt IN NUMBER,vid IN NUMBER)
Is
begin
UPDATE vehicles_tab v
SET TREAT(VALUE v As bicycle ).vehics.gear_count=gearcnt
WHERE TREAT(VALUE v As bicycle).vehics.vehicle_id = vid;
end;
END;
/
INSERT INTO vehicles_tab(vehics)
values(bicylce(vehic_seqx.NEXTVAL,'AVC','06-AUG-08','BLUE',5
));
INSERT INTO Vehicles_tab (vehics) значения (truck (vehicle_seqx.NEXTVAL, 'MERCEDEZ', '12-AUG-11', 'BLACK', '', '', '', 34));
ВЫБРАТЬ v.vehics.get_vehicle () из транспортных средств_таб v;
SET SERVEROUTPUT ON;
declare
`` v_bicycle Bicycle: = новый велосипед ('1-2019', 'AVC', '06 -AUG-08' , 'СИНИЙ', 5);
`` begin
v_bicycle.set_gear_count('1-2019',9);
end;
SELECT v.vehics.get_vehicle() from vehicles_tab v;