子类

时间:2019-08-30 09:16:33

标签: constructor abstract-class abap

这是我的抽象超类:

class ZMP_ACAR definition
  PUBLIC
  ABSTRACT
  create public .

public section.
  DATA: maxSeats TYPE i,
        maxKidSeats TYPE i,
        carKidSeats TYPE TABLE OF REF TO z280819_kidseat,
        carPeople TYPE TABLE OF REF TO z280819_person.
  METHODS CONSTRUCTOR
    IMPORTING maxSeats TYPE i
              maxKidSeats TYPE i.
  METHODS goToTrip ABSTRACT.
protected section.
private section.
ENDCLASS.

CLASS ZMP_ACAR IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    me->maxSeats = maxSeats.
    me->maxKidSeats = maxKidSeats.
  ENDMETHOD.
ENDMETHOD.
ENDCLASS.

这是我的子类,它继承自上面的抽象超类

class Z30082019_BMWCAR definition
  public
  inheriting from ZMP_ACAR
  final
  create public .

public section.
  METHODS CONSTRUCTOR
    IMPORTING maxSeats TYPE i
              maxKidSeats TYPE i.
  METHODS addKidSeatToCar REDEFINITION.
  METHODS goToTrip REDEFINITION.
protected section.
private section.
ENDCLASS.

CLASS Z30082019_BMWCAR IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    super->constructor( ).  "--> ERROR: No value was passed to the mandatory parameter "MAXSEATS".

    me->maxSeats = maxSeats.
    me->maxKidSeats = maxKidSeats.
  ENDMETHOD.

  METHOD goToTrip.
    ... 
  ENDMETHOD.
ENDCLASS.

它在Z30082019_BMWCAR的构造函数实现内引发错误-上面的行: ERROR: No value was passed to the mandatory parameter "MAXSEATS"

你能告诉我是什么问题吗?

2 个答案:

答案 0 :(得分:1)

从基类(ZMP_ACAR)构造函数的签名中可以看出,它具有两个强制(非可选)导入参数。在实例化派生类(Z30082019_BMWCAR)时提供它们。将多个参数传递给函数时,ABAP语法有点肿,但这应该可以工作:

CLASS Z30082019_BMWCAR IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    super->constructor( EXPORTING maxSeats = maxSeats
                                  maxKidSeats = maxKidSeats ). 
    ""// me->maxSeats and me->maxKidSeats are now inherited (public) instance variables from the super class.
  ENDMETHOD.

答案 1 :(得分:1)

U也可以从Prelude> deal [1,2,3,4,5,6,7] ([1,3,5,7],[2,4,6]) 中完全删除构造函数,并将其放入ZMP_ACAR中。不太优雅,但可以。

Z30082019_BMWCAR
class ZMP_ACAR definition
  PUBLIC
  ABSTRACT
  create public .

public section.
  DATA: maxSeats TYPE i,
        maxKidSeats TYPE i,
        carKidSeats TYPE TABLE OF REF TO z280819_kidseat,
        carPeople TYPE TABLE OF REF TO z280819_person.
  METHODS goToTrip ABSTRACT.
protected section.
private section.
ENDCLASS.

CLASS ZMP_ACAR IMPLEMENTATION.

ENDCLASS.