「Java入門」cloneメソッドの使い方

説明
1.Cloneableインターフェイスを実施していないインスタンスを複製する場合、CloneNotSupportedExceptionが出力される
2.戻り値はObject型

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class UserExample implements Cloneable {
protected String userId;
private int age;
public UserExample clone() {
try {
return (UserExample)super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
}
}
}
public class UserExample implements Cloneable { protected String userId; private int age; public UserExample clone() { try { return (UserExample)super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e.toString()); } } }
public class UserExample implements Cloneable {

protected String userId;
private int age;

public UserExample clone() {
            try {
                  return (UserExample)super.clone();
            } catch (CloneNotSupportedException e) {
                  throw new InternalError(e.toString());
            }
    }

}

 

Java

Posted by arkgame