
内容导航:
- 前言
- 1、clone()方法
1、clone()方法
如果一个类或它的超类之一实现了“Cloneable”接口,那么可以使用“clone()”方法从现有对象创建副本。要创建一个克隆,我们可以写:
CloneableObject.clone();
该方法的实现将检查调用clone()的对象是否实现了Cloneable接口。如果对象没有,该方法将抛出一个CloneNotSupportedException异常。异常处理将在后面的课程中介绍。目前,需要知道clone()必须被声明为
protected Object clone() throws CloneNotSupportedException
或者:
public Object clone() throws CloneNotSupportedException
持续更新,欢迎留言提议!
码字很累,多点赞多赞赏!

网友评论