問題描述
如何重載對像類型中的方法 (How to overload a method in an object type)
我在對象 car
中有一個簡單的方法,它將功率從對象的馬力字段 (hp) 中的值轉換為千瓦 (kW)。代碼如下:
MAP MEMBER FUNCTION engine_power
RETURN FLOAT IS
v_kw FLOAT := 0.745699872;
BEGIN
RETURN hp * v_kw;
END;
現在我想重載這個方法來計算冪,但是結果會是INTEGER而不是FLOAT。
重載的語法是什麼對象主體中的方法?
參考解法
方法 1:
You can only have one MAP method, but you can add another method with the integer. You need to add it in both the type and type body statements:
create type my_object as object (hp number,
MAP MEMBER FUNCTION engine_power RETURN FLOAT,
MEMBER FUNCTION engine_power RETURN INTEGER
);
/
Type MY_OBJECT compiled
create type body my_object as
MAP MEMBER FUNCTION engine_power
RETURN FLOAT IS
v_kw FLOAT := 0.745699872;
BEGIN
RETURN hp * v_kw;
END;
MEMBER FUNCTION engine_power
RETURN INTEGER IS
v_kw FLOAT := 0.745699872;
BEGIN
RETURN TRUNC(hp * v_kw);
END;
end;
/
Type body MY_OBJECT compiled
I'm not sure you really want either to be a MAP method, but depends how you're using the methods.
(by Caroso、Alex Poole)