Pregunta

Necesito almacenar crear un nuevo tipo para números racionales representados como numerador/denominador para almacenar fracciones como 3/5. Encontré lo siguiente para hacerlo.

CREATE TYPE Rational AS OBJECT ( 
   num INTEGER,
   den INTEGER,
   MAP MEMBER FUNCTION convert RETURN REAL,
   MEMBER PROCEDURE normalize,
   MEMBER FUNCTION reciprocal RETURN Rational,
   MEMBER FUNCTION plus (x Rational) RETURN Rational,
   MEMBER FUNCTION less (x Rational) RETURN Rational,
   MEMBER FUNCTION times (x Rational) RETURN Rational,
   MEMBER FUNCTION divby (x Rational) RETURN Rational,
   PRAGMA RESTRICT_REFERENCES (DEFAULT, RNDS,WNDS,RNPS,WNPS)
);

¿Cómo puedo agregar latinta de que el denominador no puede ser cero?

¿Fue útil?

Solución

Deberá declarar un constructor para su tipo. Entonces puedes poner cualquier validación que quieras en el cuerpo.

CREATE TYPE Rational AS OBJECT ( 
   num INTEGER,
   den INTEGER,
   MAP MEMBER FUNCTION convert RETURN REAL,
   MEMBER PROCEDURE normalize,
   MEMBER FUNCTION reciprocal RETURN Rational,
   MEMBER FUNCTION plus (x Rational) RETURN Rational,
   MEMBER FUNCTION less (x Rational) RETURN Rational,
   MEMBER FUNCTION times (x Rational) RETURN Rational,
   MEMBER FUNCTION divby (x Rational) RETURN Rational,
   constructor function rational
            (n integer, d integer)
            return self as result,    
   PRAGMA RESTRICT_REFERENCES (DEFAULT, RNDS,WNDS,RNPS,WNPS)
);

En su tipo de cuerpo, agregue el código del constructor:

constructor function rational
            (n integer, d integer)
            return self as result
is     
begin
     if d = 0 then
         raise_application_error(-20000, 'Denominator cannot be zero!');
     end if;
     self.num := n;
     self.den := d;
end rational; 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top