Created
October 15, 2018 08:45
-
-
Save jrmmendes/8f9a19f268397917fc5e6f5b0707cb98 to your computer and use it in GitHub Desktop.
Utilização do método clone() através da implementação de Cloneable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Code by José Romildo M. Júnior <[email protected]> | |
*/ | |
package snippets; | |
class Example implements Cloneable { | |
private double d; | |
private String name; | |
public Example(double d, String name) { | |
this.d = d; | |
this.name = name; | |
} | |
public void setD(double d) { | |
this.d = d; | |
} | |
public double getD() { | |
return d; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public Example getClone() throws CloneNotSupportedException { | |
return (Example) this.clone(); | |
} | |
} | |
public class Snippets { | |
public static void main(String[] args) throws CloneNotSupportedException { | |
Example a = new Example(1, "A"); | |
Example b = new Example(2, "B"); | |
Example c = new Example(3, "C"); | |
Example[] exArray = {a, b, c}; | |
Example[] anotherArray = new Example[exArray.length]; | |
for (int i = 0; i < exArray.length; i++) { | |
anotherArray[i] = exArray[i].getClone(); | |
} | |
anotherArray[0].setD(0); | |
anotherArray[0].setName("Mudou"); | |
System.out.println(exArray[0].getD() + exArray[0].getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment