[OO] ABAP OO Syntax--examples see the difference between "=" and "? =" [Go]

Source: Internet
Author: User

ABAP OO Object-oriented syntax
Examples see the difference between "=" and "? ="
1. "=" Assignment of the same type
*&---------------------------------------------------------------------*
*& Report Z_wys_typeref01
*& Author: Yong Shang
*&---------------------------------------------------------------------*
*& between peers with = Assignment
*&
*&---------------------------------------------------------------------*report z_wys_typeref01. *----------------------------------------------------------------------*
* CLASS CP DEFINITION
*----------------------------------------------------------------------*
*c_parent definition
*----------------------------------------------------------------------*
Class C_parent DEFINITION. "Parent class
Public section.
DATA i TYPE i.
DATA s TYPE string.
METHODS constructor.
METHODS Zwrite. PRIVATE section.
Endclass. "A DEFINITION
*----------------------------------------------------------------------*
* CLASS c_parent implementation
*----------------------------------------------------------------------*
*c_parent implementation
*----------------------------------------------------------------------*
CLASS c_parent implementation.
METHOD constructor.
WRITE/' parent class created! '.
EndMethod. "Constructor
METHOD Zwrite.
Write/' Parent-class write. '
EndMethod. "destructor
"destructor
Endclass. "A start-of-selection."
DATA CP TYPE REF to C_parent. "
DATA cp2 TYPE REF to C_parent. "
CREATE OBJECT CP. "CP instantiation, the system creates a
"An instance of the C_parent type (for example, named C_parent_1)
"And let CP refer to this instance

Cp->zwrite (). The Call member function
CP2 = CP.                      "Statically declared CP and CP2 are the same type. "CP2 is not instantiated (C_parent_2 is not created in the background)," CP2 simply refers to an instance of the CP reference.
Cp->i = 10. The cp2->i was replaced with 10.  This is the charm of reference type ^_^ write:/cp->i,cp2->i.         Cp2->i = 99. The cp->i was replaced with 99. This is also a reference type of glamour ^_^
Write:/Cp->i,cp2->i.
Cp2->zwrite (). The Call member function
2. "=" upward transformation
*&---------------------------------------------------------------------*
*& Report Z_wys_typeref
*&
*&---------------------------------------------------------------------*
*&
*& base class = derived class
*&---------------------------------------------------------------------*report z_wys_typeref02. *----------------------------------------------------------------------*
* CLASS CP DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
Class C_parent DEFINITION. "Base class
Public section.
DATA i TYPE i.
DATA s TYPE string.
METHODS constructor.
METHODS Zwrite. PRIVATE section.
Endclass. "A DEFINITION
*----------------------------------------------------------------------*
* CLASS c_parent implementation
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent implementation.
METHOD constructor.
The parent class creates
WRITE/' parent class created! '.
EndMethod.
METHOD Zwrite.
Write/' Parent-class write. '
EndMethod. "destructor
"destructor
Endclass. "A
*----------------------------------------------------------------------*
* CLASS C_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child DEFINITION inheriting from C_parent.
Public section.
DATA child_str TYPE String.
METHODS constructor.
METHODS zwrite redefinition.
Endclass. "*----------------------------------------------------------------------*
* CLASS AC Implementation
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C_child implementation.
METHOD constructor. The subclass creates
Super->constructor (). " Call the constructor of the parent class first
WRITE ' subclass Create! '.
EndMethod.
METHOD Zwrite.
Write/' Subclass write '.
EndMethod.                    "Destructorendclass. "AC implementationstart-of-selection.
DATA CP TYPE REF to C_parent. "
DATA cc TYPE REF to C_child. "
Create OBJECT cc. "Creating Child class instances
CP = CC. "Parent class = Subclass
"=" on both sides of "static declaration" type mismatch,
"But" = "Left is the base class on the right
"So the grammar check through
Cp->zwrite (). "Methods for calling subclasses
3. "? =" downward transformation
*&---------------------------------------------------------------------*
*& Report Z_wys_typeref
*&
*&---------------------------------------------------------------------*
*&
*& derived class = base class
*&---------------------------------------------------------------------*report z_wys_typeref03. *----------------------------------------------------------------------*
* CLASS CP DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
Class C_parent DEFINITION. "Parent class
Public section.
DATA i TYPE i.
DATA s TYPE string.
METHODS constructor.
METHODS Zwrite. PRIVATE section.
Endclass. "A DEFINITION
*----------------------------------------------------------------------*
* CLASS c_parent implementation
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_parent implementation.
METHOD constructor.
The parent class creates
WRITE/' parent class created! '.
EndMethod.
METHOD Zwrite.
Write/' Parent-class write. '
EndMethod. "destructor
"destructor
Endclass. "A
*----------------------------------------------------------------------*
* CLASS C_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_child DEFINITION inheriting from C_parent.
Public section.
DATA child_str TYPE String.
METHODS constructor.
METHODS zwrite redefinition.
Endclass. "*----------------------------------------------------------------------*
* CLASS AC Implementation
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C_child implementation.
METHOD constructor. The subclass creates
Super->constructor (). " Call the constructor of the parent class first
WRITE ' subclass Create! '.
EndMethod.
METHOD Zwrite.
Write/' Subclass write '.
EndMethod.                    "Destructorendclass. "AC implementationstart-of-selection.
DATA CP TYPE REF to C_parent. "
DATA cc TYPE REF to C_child. "*******************************************************
* ERROR 1 "Create OBJECT CP." Creating a parent class instance
"CC = CP." Subclass = Parent class Syntax check error
"=" in static declaration does not match on both sides of the type.
**************************************************************************************************************
* ERROR 2 "Create OBJECT CP." Creating a parent class instance
"CC? = CP." Syntax check correct, but run-time type mismatch
That's right
Create OBJECT CP type C_child. "Dynamically created CP for C_child
CC? = CP. "Static syntax check correct, dynamic type consistent
Cc->zwrite (). "Methods for calling subclasses


Examples are complete.
About "? =", as explained in SAP Help:

MOVE Source {to|? to} destination.
Destination {=|? =} source.
Original:
Both These statements assign the content of the operand source to
The data object destination. The variants with the addition to or
The assignment operator = is valid for all assignments between
Operands that is not reference variables,
And for assignments between reference variables for which the
Static type of source is more specific than or the same as the
Static type of destination (narrowing cast).
Chinese:
The above two declarations are assigned a value (destination) to the target data object using the source operand's data (source).
The "to" or assignment operator "=" Applies to the following two scenarios:
1. Operands, including source and target objects, are not reference types
2. If it is a reference type, the source object and the target object type are the same (example 1 in this article) or the source object is more specific than the target object (upward transformation)--(example 2 in this article)


Original:
Variants with the addition? To or the assignment operator? = (casting operator)
Must be used if the source and destination is reference variables
And the static type of source is more general than the static type
of destination (down cast).
For assignments between operands that is not reference variables,
Use of the question mark? is not permitted.
Chinese:
Use the? To "or assignment operator"? = "(Type conversion operator) of the variable, to have the following two conditions:
1. Operands, including source and target objects, are reference types;
2. Also, the static type of the source object (the type declared by the data statement) is more abstract than the target object (transformation downward).--(example 3 in this article)
A non-reference variable assignment cannot be assigned with the question mark operator.

[OO] ABAP OO Syntax--examples see the difference between "=" and "? =" [Go]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.