Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package punct;
- public class Punct2D extends Object implements Cloneable { //catre clasa Clone pentru a nu mai arunca exceptie
- private double x;
- private double y;
- private String eticheta;
- static final double X_ORIG = 0; //final adica const
- static final double Y_ORIG = 0;
- static final String ETICHETA = "Origine";
- public Punct2D(double x, double y, String eticheta) {
- this.x = x; //este o eticheta a parametrului x, nu un pointer ca in C++
- this.y = y;
- this.eticheta = eticheta;
- }
- public Punct2D(){
- this.x=X_ORIG;
- this.y=Y_ORIG;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- @Override
- public String toString() {
- // return "Punct2D{" +
- //// "x=" + x +
- //// ", y=" + y +
- //// ", eticheta='" + eticheta + '\'' +
- //// '}';
- return this.x + "," + this.y + "," + this.eticheta; // pentru fisiere, citire, scriere
- }
- public void setX(double x) {
- this.x = x;
- }
- public void setY(double y) {
- this.y = y;
- }
- public void setEticheta(String eticheta) {
- this.eticheta = eticheta;
- }
- @Override
- protected Object clone() throws CloneNotSupportedException { //in spate aloca memorie si daca nu mai este... arunca exceptie
- return super.clone();
- }
- }
- package punct;
- public class UtilizarePunct2D {
- public static void main(String[] args){
- Punct2D p1 = new Punct2D();
- System.out.println(p1.toString());
- // Punct2D p2 = p1; //pointeaza catre adresa lui p1;
- //
- // System.out.println(p2.toString());
- // p2.setEticheta("Punct 2");
- // System.out.println(p2.toString());
- // System.out.println(p1.toString());
- try {
- Punct2D p3= (Punct2D) p1.clone();
- System.out.println(p3.toString());
- p3.setEticheta("Punct 3");
- System.out.println(p3.toString());
- System.out.println(p1.toString());
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement