‎2007 Jul 12 3:54 PM
Hi,
Can one define a method so that is can be used as both a static or an instance method?
Basically I'm trying to simplify my class to so that I can call a method either statically with parameters or instantiated using it's own attributes.
In other words, I'm trying to accomplish both of these with one method:
zcl_myclass=>do_something_static( im_key = '1234' ).
lv_myclass_instance->do_something( ). " key in private attributesWhy would I want to do that?
I would like to avoid instantiation in some cases for performance reasons and would like to keep it simple by having only one method to do basically the same thing.
Any input or alternative suggestions welcome.
Cheers,
Mike
‎2007 Jul 12 4:20 PM
No sure if I'm following you 100%, but you can always call a static method from a object reference. See here.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app definition.
public section.
class-methods: do_something importing im_str type string.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app implementation.
method do_something.
write:/ 'Do_Something - ', im_str.
endmethod.
endclass.
data: o_app type ref to lcl_app.
start-of-selection.
create object o_app.
call method o_app->do_something( 'Instansiated' ).
call method lcl_app=>do_something( 'Static' ).
Regards,
Rich Heilman
‎2007 Jul 12 4:20 PM
No sure if I'm following you 100%, but you can always call a static method from a object reference. See here.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app definition.
public section.
class-methods: do_something importing im_str type string.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app implementation.
method do_something.
write:/ 'Do_Something - ', im_str.
endmethod.
endclass.
data: o_app type ref to lcl_app.
start-of-selection.
create object o_app.
call method o_app->do_something( 'Instansiated' ).
call method lcl_app=>do_something( 'Static' ).
Regards,
Rich Heilman
‎2007 Jul 12 4:30 PM
AFAIK you can't do that.
To avoid redundant code (always recommendable) you can however design it like that:
static method do_something static importing im_key ...
instance method do_something.
call method zcl_myclass=>do_something_static exporting im_key = me->my_private_attribute.
endmethod.
Regards
Frank
‎2007 Jul 12 4:58 PM
Thanks for the input. The problem is I cannot use instance data if it is declared as static. What may also be relevant is that this is a public class using the class builder.
Inside the method I would like to test for instantiation and using instance data if available, thus:
method do_something.
if im_param is supplied.
" use parameter data
elseif me is bound. " wishful thinking...
" use instance data
else. raise exception. endif.
" do stuff
endmethod.I was just trying to avoid the rather clumsy look of having two methods doing the same thing (actually several of these in several classes). Or the equally clumsy-looking
lv_myobj->do_something( im_parm1 = lv_myobj->attr1
im_parm2 = lv_myobj->attr2
..... )So far I'll probably stick with a static and pass all the instance data through for each call, but will leave the question open for a little while to see if any other suggestions surface.
Cheers,
Mike
‎2007 Jul 12 6:04 PM
Ok, I may be reaching here a bit, I know, but maybe this may give you some ideas. After creating the object, pass it back to the method call.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app definition.
public section.
data: a_attribute type string.
class-methods: do_something importing im_str type string
im_ref type ref to lcl_app optional.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app implementation.
method do_something.
if not im_ref is initial.
im_ref->a_attribute = im_str.
write:/ 'Do_Something - ', im_ref->a_attribute.
else.
write:/ 'Do_Something - ', im_str.
endif.
endmethod.
endclass.
data: o_app type ref to lcl_app.
start-of-selection.
create object o_app.
call method o_app->do_something(
exporting
im_str = 'Instansiated'
im_ref = o_app ).
call method lcl_app=>do_something(
exporting
im_str = 'Static' ).
Regards,
Rich Heilman
‎2007 Jul 13 6:14 AM
Hi
Purposes are different for Static and Instance methodology.. You can not use single method for both purpose.
You can have static data and static method, which can be access with an instance, but it can not be different for different instances, like instance methods and instance data.
Two concepts are totally different for different purposes.
thanks,
Chetan Shah
‎2007 Jul 13 3:00 PM
Thanks for the input, at least it provided some food for thought. Though hoping for a more elegant solution, I ended up just passing all the data in anyway and treating it as a pure static.
Cheers,
Mike