Can an abstract class have a constructor?

前端 未结 22 2519
甜味超标
甜味超标 2020-11-22 05:25

Can an abstract class have a constructor?

If so, how can it be used and for what purposes?

22条回答
  •  心在旅途
    2020-11-22 06:08

    In a concrete class, declaration of a constructor for a concrete type Fnord effectively exposes two things:

    • A means by which code can request the creation of an instance of Fnord

    • A means by which an instance of a type derived from Fnord which is under construction can request that all base-class features be initialized.

    While there should perhaps be a means by which these two abilities could be controlled separately, for every concrete type one definition will enable both. Although the first ability is not meaningful for an abstract class, the second ability is just as meaningful for an abstract class as it would be for any other, and thus its declaration is just as necessary and useful.

提交回复
热议问题