2007 Feb 03 9:56 AM
hi all,
i read the book written by dr horst keller and sascha kruger "abap objects" with confusion on the below script. really hope can get the help.
1) confused 1, why creator has no create object and only create reference variable? why no need to create instance of client class?
2) confused 2. why only me? me means everything(attribute/method) of client class? why not define as me->name just like in confused 4?
3) why creator no create object also can creator->name?
report self_reference.
class client definition.
public section.
data name(10) type c value 'master' read-only.
methods create_server.
endclass.
class server definition.
public section.
methods acknowledge importing creator type ref to client. "confused 1
private section.
data name(10) type c value 'servant'.
endclass.
class client implementation.
method create_server.
data server_ref type ref to server.
create object server_ref.
call method server_ref->acknowledge exporting creator = me. "confused 2
endmethod.
endclass.
class server implementation.
method acknowledge.
data name type string.
name = creator->name. "confused 3
write: me->name, 'created by', name. "confused 4
endmethod.
endclass.
data client_ref type ref to client.
start-of-selection.
create object client_ref.
call method client_ref->create_server
the output is "servant created by master".
hi all,
i read the book written by dr horst keller and sascha kruger "abap objects" with confusion on the below script. really hope can get the help.
1) confused 1, why creator has no create object and only create reference variable? why no need to create instance of client class?
2) confused 2. why only me? me means everything(attribute/method) of client class? why not define as me->name just like in confused 4?
3) why creator no create object also can creator->name?
report self_reference.
class client definition.
public section.
data name(10) type c value 'master' read-only.
methods create_server.
endclass.
class server definition.
public section.
methods acknowledge importing creator type ref to client. "confused 1
private section.
data name(10) type c value 'servant'.
endclass.
class client implementation.
method create_server.
data server_ref type ref to server.
create object server_ref.
call method server_ref->acknowledge exporting creator = me. "confused 2
endmethod.
endclass.
class server implementation.
method acknowledge.
data name type string.
name = creator->name. "confused 3
write: me->name, 'created by', name. "confused 4
endmethod.
endclass.
data client_ref type ref to client.
start-of-selection.
create object client_ref.
call method client_ref->create_server
the output is "servant created by master".
2007 Feb 03 11:22 PM
hi e l,
the program starts with
start-of-selection.
create object client_ref.
Create Object creates an instance of the class client and returns a reference to this in the reference variable client_ref.
Then
call method client_ref->create_server
calls the method create_server of the instance of class client.
this method creates an instance of class server and returns the reference to this instance in reference variable server_ref. The method acknowledge is called with export parameter ME. ME is a reference to the instance of the class itself, in this case client.
Method acknowledge has a local variable called name. This name takes the value of attribute name of the imported refernece creator. Creator is an instance of object client. Client has an attribute name type c value 'master' in the public section. That means: acknowledge local data field name takes value 'master' .
me->name points to the attribute name in private section of class server definition.
it is defined as data name(10) type c value 'servant'.
Thus
write: me->name, 'created by', name.
results in "servant created by master".
I think this is a good example how to confuse people using the same field names in very different contexts.
Regards,
Clemens
2007 Feb 04 12:35 AM
hi,
thanks for the reply.
i actually still not clear of the following
1) why creator has no [create object]?
2) ME is a reference to the instance of the class itself. so when in the class where ME is defined, it is referring to itself. may know when only has ME (without the operator '->', it means everything be it attribute or method in public or private section also can reference?
thanks
2007 Feb 05 5:37 AM
2007 Feb 05 9:50 AM
2007 Feb 05 11:23 AM
hi,
i will explain your confusion along with your example...
report self_reference.
class client definition.
public section.
data name(10) type c value 'master' read-only.
methods create_server.
endclass.
class server definition.
public section.
methods acknowledge importing creator type ref to client. "confused 1
private section.
data name(10) type c value 'servant'.
endclass.
class client implementation.
method create_server.
data server_ref type ref to server.
create object server_ref.
call method server_ref->acknowledge exporting creator = me. "confused 2
endmethod.
endclass.
class server implementation.
method acknowledge.
data name type string.
name = creator->name. "confused 3
write: me->name, 'created by', name. "confused 4
endmethod.
endclass.
data client_ref type ref to client.
start-of-selection.
create object client_ref.
call method client_ref->create_server
the output is "servant created by master".
confusion 1:
Normaly while creating an object we have to create a reference variable to class and then create the instance of the class with 'Create Objec' statement, but in this example we are passing a parameter creator of type client is sent to acknowledge method of server class. Here the parameter creator is sent as an reference variable which means that the object is already created and just an reference to that variable is sent to the method acknowledge.
confusion 2:
here 'me' is a self reference variable of a class. It does not mean an attribute or methods of a class, it denotes the object of the class itself in which it is referenced. Normally the 'me' is used for self reference inside the class methods for refering to attributes or methods of the same class.
eg: me->attrib1 or call method me->method1
confusion 3:
An ABAP class has two parts namely a defenition part and an implementaion part. The defenition part of the class contains all the attributes and declaration of the methods, whereas the implementation part contains the actual implementation of methods i.e., defenition of methods. In simple words, the defenition part of the class contains the methods name and what are the parameters like exporting, importing, changing,exceptions, etc., whereas the implementation part contains actual functionality of the method.
As i have already stated that the parameter passed to the method are passed as reference variable, there is no need for creating object seperately and just an reference to already exisiting object is passed as parameter.
so we can obtain the value of the attribute 'name' by means of the reference variable creator which gives the value of the client object.
confusion 4:
'me' is a self reference variable which is nothing but a reference to the same class, so me->name refers to the attribute name of the server class.
2007 Feb 05 11:50 AM
hi prosper,
really appreciate your effort writing so much. i will reward you and others accordingly.
thanks
2007 Feb 05 11:50 AM
2007 Feb 05 11:50 AM