如果超类已经做到了,子类是否需要显式重写Clone()方法? [英] Does subclass need to override Clone() method explicitly if superclass has already done it

查看:82
本文介绍了如果超类已经做到了,子类是否需要显式重写Clone()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果类A扩展了类B,并且类B已经实现了可克隆的接口,则类A是否必须声明'clone()throws CloneNotSupportedException'。

If Class A extends class B and class B has already implemented cloneable interface then is it necessary for class A to declare 'clone() throws CloneNotSupportedException' .

I猜测它应该不是强制性的,因为克隆类A的对象的属性将自动从类B继承。

I guess it should not be mandatory, as property to clone Objects of class A would automatically be inherited from Class B.

推荐答案

如果类B定义了非原始可变成员字段,则必须覆盖 clone()。需要在 B.clone()中明确复制这些内容。如果B仅包含原始和/或不可变的数据成员,则 A.clone()将完成该工作。

It is necessary to override clone() if class B defines non-primitve mutable member fields. These need to be deep copied explicitly within B.clone(). If B only contains primitive and/or immutable data members, A.clone() will do the job.

有关更详细的说明,请参见我的早期答案一个类似的问题。

For a more detailed explanation, see this earlier answer of mine to a similar question.

这篇关于如果超类已经做到了,子类是否需要显式重写Clone()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆