CREATE OR REPLACE TYPE food_ot AS OBJECT
(
name VARCHAR2 (100),
food_group VARCHAR2 (50),
grown_in VARCHAR2 (100),
MEMBER FUNCTION price
RETURN NUMBER
)
NOT FINAL;
Type created.
CREATE OR REPLACE TYPE BODY food_ot
IS
MEMBER FUNCTION price
RETURN NUMBER
IS
BEGIN
DBMS_OUTPUT.put_line ('Food price!');
RETURN ( LENGTH (self.name)
+ LENGTH (self.food_group)
+ LENGTH (grown_in))
* 10;
END;
END;
Type created.
DECLARE
l_food food_ot := food_ot ('Ramen Noodle Soup', 'Salt', 'Microwave');
BEGIN
DBMS_OUTPUT.put_line ('What do we see?');
DBMS_OUTPUT.put_line (
'The price of ' || l_food.name || ' = ' || l_food.price());
END;
Statement processed.
What do we see?
Food price!
The price of Ramen Noodle Soup = 300
CREATE OR REPLACE TYPE dessert_ot UNDER food_ot (
contains_chocolate CHAR (1),
year_created NUMBER (4)
)
NOT FINAL;
Type created.
DECLARE
/* M is for Maybe */
l_dessert dessert_ot
:= dessert_ot ('Ice Cream',
'Sugar',
'Cow',
'M',
200);
BEGIN
DBMS_OUTPUT.put_line ('What do we see?');
DBMS_OUTPUT.put_line (
'The price of ' || l_dessert.name || ' = ' || l_dessert.price());
END;
Statement processed.
What do we see?
Food price!
The price of Ice Cream = 170
CREATE OR REPLACE TYPE dessert_ot
UNDER food_ot
(
contains_chocolate CHAR (1),
year_created NUMBER (4),
OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
)
NOT FINAL;
Type created.
CREATE OR REPLACE TYPE dessert_ot UNDER food_ot (
contains_chocolate CHAR (1)
, year_created NUMBER (4)
, OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
)
NOT FINAL;
Type created.
CREATE OR REPLACE TYPE BODY dessert_ot
IS
OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
IS
multiplier NUMBER := 1;
BEGIN
DBMS_OUTPUT.put_line ('Dessert price!');
IF SELF.contains_chocolate = 'Y'
THEN
multiplier := 2;
END IF;
IF SELF.year_created < 1900
THEN
multiplier := multiplier + 0.5;
END IF;
RETURN (10.00 * multiplier);
END;
END;
Type created.
DECLARE
l_food food_ot := food_ot ('Ramen Noodle Soup', 'Salt', 'Microwave');
/* M is for Maybe */
l_dessert dessert_ot
:= dessert_ot ('Ice Cream',
'Sugar',
'Cow',
'M',
200);
BEGIN
DBMS_OUTPUT.put_line ('What do we see?');
DBMS_OUTPUT.put_line (
'The price of ' || l_food.name || ' = ' || l_food.price());
DBMS_OUTPUT.put_line (
'The price of ' || l_dessert.name || ' = ' || l_dessert.price());
END;
Statement processed.
What do we see?
Food price!
The price of Ramen Noodle Soup = 300
Dessert price!
The price of Ice Cream = 15
CREATE OR REPLACE TYPE BODY dessert_ot
IS
OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
IS
multiplier NUMBER := 1;
BEGIN
DBMS_OUTPUT.put_line ('Dessert price!');
IF self.contains_chocolate = 'Y'
THEN
multiplier := 2;
END IF;
IF self.year_created < 1900
THEN
multiplier := multiplier + 0.5;
END IF;
RETURN (10.00 * multiplier);
END;
END;
Type created.
DECLARE
l_food food_ot := food_ot ('Ramen Noodle Soup', 'Salt', 'Microwave');
/* M is for Maybe */
l_dessert dessert_ot
:= dessert_ot ('Ice Cream',
'Sugar',
'Cow',
'M',
200);
BEGIN
DBMS_OUTPUT.put_line ('What do we see?');
DBMS_OUTPUT.put_line (
'The price of ' || l_food.name || ' = ' || l_food.price());
DBMS_OUTPUT.put_line (
'The price of ' || l_dessert.name || ' = ' || l_dessert.price());
END;
Statement processed.
What do we see?
Food price!
The price of Ramen Noodle Soup = 300
Dessert price!
The price of Ice Cream = 15