Effective Java: Analysis of the clone() method

前端 未结 5 663
南笙
南笙 2020-12-09 04:48

Consider the following from Effective Java Item 11 (Override clone judiciously) where Josh Bloch is explaining what is wrong with the clone() contract .

5条回答
  •  长情又很酷
    2020-12-09 05:03

    If a class is not final, clone has to return the most derived class for which it was called. That can't work with a constructor, because clone doesn't know which one to call. If a class is final, it can't have any subclasses, so there's no danger in calling its constructor when cloning.

提交回复
热议问题