본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

Clone

Clone 메소드 사용


 

class Car implements Cloneable{
 String model;
 String company;
 public Car(String model, String company) {
  super();
  this.model = model;
  this.company = company;
 }
 public Car() {}
 @Override
 public String toString() {
  return "Car [model=" + model + ", company=" + company + "]";
 }
 public void test() throws CloneNotSupportedException{
  // clone 사용 작성 : protected ; 상속클래스 사용
  Car copy = (Car)(this.clone());
  copy.model = "그랜져";
  System.out.println(this.model);
  System.out.println(copy.model);
 }
}

public class ToStringTest {
 public static void main(String[] args) throws CloneNotSupportedException {
  Car c1 = new Car("K7", "현대차");
  Car c2 = new Car("아반테", "기아차");
  Car c3 = new Car();
  c3.test();
  System.out.println(c1);
  System.out.println(c2);  
  }

}

 

 

ToStringTest.java