如何链接返回SELF的函数的pl/sql对象类型的调用 [英] How to chain calls in a pl/sql object type of functions returning SELF

查看:75
本文介绍了如何链接返回SELF的函数的pl/sql对象类型的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使一个oracle对象返回自身并能够链接这些调用.我该怎么办?

I want to make an oracle object return itself and be able to chain these calls. How do I do that?

我尝试返回相同的类型,但是它不起作用,我还尝试添加一个由函数调用的过程,但是它也不起作用.总是抱怨修改width成员的值.看起来函数不会接受副作用吗?是否按照更数学的函数原理建模?这是可以实现的吗?我想我可以编写该函数,以便它用SELF构建一个新的矩形,但这是很多工作.

I have tried returning the same type, but it doesnt work, I also tried adding a procedure that is invoqued by the funcition but it doesn't work either. Always complains about modifying the value of the width member. Looks like functions wont admit side effects?, are they modelled after a more mathematical function principle? Is this achievable?. I guess I could write the function so it builds a new rectangle with SELF, but that is so much work.

我的目标是能够链接像jQuery或某些Java类(单例?)之类的调用.像这样:

My goal is to be able to chain calls like jQuery or some java classes (a singleton?). Something like:

r := r.setWidth(0).setWidth(1).setWidth(2);

当然,它将有更多的方法,并且它不是矩形.这是错误:

Of course, it would have more methods and it wouldn't be a rectangle. This is the error:

Error: PLS-00363: expression 'SELF' cannot be used as an assignment target
Line: 18
Text: stWidth(w);

-

CREATE OR REPLACE TYPE rectangle AS OBJECT
(
-- The type has 3 attributes.
  length NUMBER,
  width NUMBER,
  area NUMBER,
-- Define a constructor that has only 2 parameters.
  CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER)
    RETURN SELF AS RESULT,
  MEMBER FUNCTION setWidth(w NUMBER) RETURN rectangle,
  MEMBER PROCEDURE stWidth(w NUMBER)
)

-

CREATE OR REPLACE TYPE BODY rectangle AS
  CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER)
    RETURN SELF AS RESULT
  AS
  BEGIN
    SELF.length := length;
    SELF.width := width;
-- We compute the area rather than accepting it as a parameter.
    SELF.area := length * width;
    RETURN;
  END;
  MEMBER PROCEDURE stWidth(w NUMBER) IS
  BEGIN
    self.width := w;
  END;
  MEMBER FUNCTION setWidth(w NUMBER) RETURN rectangle IS
    BEGIN
        stWidth(w);

        RETURN SELF;
  END;
END;

谢谢.

推荐答案

您不能同时更改和分配该对象.您已经知道解决方案使用SELF构建新矩形".但这不会做很多工作.

You cannot both change the object and assign to it at the same time. You already know the solution, "build a new rectangle with SELF". But it won't be a lot of work.

替换此:

  MEMBER FUNCTION setWidth(w NUMBER) RETURN rectangle IS
    BEGIN
        stWidth(w);
        RETURN SELF;
  END;

与此:

  MEMBER FUNCTION setWidth(w NUMBER) RETURN rectangle IS
      v_rectangle rectangle := self;
    BEGIN
        v_rectangle.width := w;
        RETURN v_rectangle;
  END;

您实际上遇到了编译错误.默认情况下,SELFIN参数.对stWidth的调用失败,因为它正在使用self.width := w;修改IN参数.

You were actually getting a compilation error. By default, SELF is an IN parameter. The call to stWidth failed because it was modifying an IN parameter with self.width := w;.

请参阅: http://docs.oracle. com/cd/B28359_01/appdev.111/b28371/adobjbas.htm#CHDCFEEE

SELF始终是传递给该方法的第一个参数.

SELF is always the first parameter passed to the method.

  • 在成员函数中,如果未声明SELF,则其参数模式 默认为IN.

  • In member functions, if SELF is not declared, its parameter mode defaults to IN.

在成员过程中,如果未声明SELF,则其参数模式 默认为IN OUT.默认行为不包括NOCOPY 编译器提示.

In member procedures, if SELF is not declared, its parameter mode defaults to IN OUT. The default behavior does not include the NOCOPY compiler hint.

这篇关于如何链接返回SELF的函数的pl/sql对象类型的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆