Abstract or Inheritance functions.

alebrije
2022-02-16
2022-02-16
  • alebrije - 2022-02-16

    Hello,
    I am recently working on an project with a lot of OOP, and the use of the ABTRACT keyword is still not clear for me.
    Here is my example:
    I have 3 levels of execution and i want that every level write something in an IN_OUT structure.
    My structure:

    TYPE St_Test :
    STRUCT
        tLevel1 : TIME;
        xLevel1 : BOOL;
        tLevel2 : TIME;
        xLevel2 : BOOL;
        tLevel3 : TIME;
        xLevel3 : BOOL;
    END_STRUCT
    END_TYPE
    

    My fb Level 1

    FUNCTION_BLOCK ABSTRACT Level1
    VAR_INPUT
        xEnableAll  : BOOL;
    END_VAR
    VAR_IN_OUT
        stInOut : St_Test;
    END_VAR
    
    IF xEnableAll THEN
        stInOut.tLevel1 := TIME();
        stInOut.xLevel1 := TRUE;
    END_IF
    

    My fb level 2

    FUNCTION_BLOCK ABSTRACT Level2 EXTENDS Level1
    
    IF xEnableAll THEN
        stInOut.tLevel2 := TIME()-stInOut.tLevel1;
        stInOut.xLevel2 := TRUE;
    END_IF
    

    My fb level 3

    FUNCTION_BLOCK PUBLIC Level3 EXTENDS Level2
    
    IF xEnableAll THEN
        stInOut.tLevel3 := TIME()- stInOut.tLevel2;
        stInOut.xLevel3 := TRUE;
    END_IF
    

    and finally my execution:
    ~~~
    PROGRAM PLC_PRG
    VAR
    fbAplication: Level3;
    stOne : St_Test;
    xEnable: BOOL;
    END_VAR

    fbAplication(xEnableAll := xEnable, stInOut := stOne);
    ~~~
    I want to achieve that whenever I call the level 3 function, I can see in my structure the changes from every level, Right now I only see like the only executed level is the level 3.
    The reason that Im doing this is because I would like to have a functionX that need the same logic from level 1 and 2 but different at level 3
    Could somebody help me to clarify this and how can I execute that with one call I have all the inheritance functions working.

     
  • alebrije - 2022-02-16

    Update:
    I found that to execute the logic of inherited functions, the command SUPER^(stInOut :=stInOut) can execute the logic of the inherited function

     
    πŸ‘
    1
  • hermsen

    hermsen - 2022-02-16

    Keyword ABSTRACT means you may NOT instanciate that CLASS (FB) immediately, you must first derive it before usage! See co5e coList library home for a detailed example.

    Search for Keywords THIS and SUPER which are special pointers.
    THIS always points to your current instance, while SUPER will always point to the parent of the current instance.

     

Log in to post a comment.