2013-04-05 3 views
0

========================================================================= ================================= 추상 슈퍼 클래스의 클론을 오버라이드

그래서 나와 함께 곰하시기 바랍니다 자바에 새로운 조금 해요 :)

나는 수퍼 클래스를 만들었습니다.

public abstract class Employee 

는 나는 내가 단지 생성자와 서브 클래스를

public class Employee_Subclass extends Employee 

을 만들었습니다

@Override 
    public Employee clone() 
    { 
     Employee foo; 

     try 
     { 
      foo = (Employee) super.clone(); 
     } 
     catch (CloneNotSupportedException e) 
     { 
      throw new AssertionError(e); 
     } 

     return foo; 
    } 

다음을 수행하여 개체 복제를 무시하려고합니다.

나는 내 주요 프로그램이 있습니다. 내가 실패, Employee_Subclass의 객체를 복제하기 위해 노력하고있어 메인 프로그램에서

.

슈퍼 클래스 만 복제 기능을 가진 하위 클래스의 객체를 복제 할 수 있는가?

내가 나를

Exception in thread "main" java.lang.AssertionError: java.lang.CloneNotSupportedException: test_project.Employee_Subclass 
    at test_project.Employee.clone(Employee.java:108) 
    at test_project.Test_Project.main(Test_Project.java:22) 
Caused by: java.lang.CloneNotSupportedException: test_project.Employee_Subclass 
    at java.lang.Object.clone(Native Method) 
    at test_project.Employee.clone(Employee.java:104) 
    ... 1 more 
Java Result: 1 

내가 제대로 것을 할 수있는 방법을 어떤 생각으로 던진 않고 AssertionError가 계속?

감사합니다.

============================================== ==================================== 내가 복제 가능한 추가 있도록

좋아, 이것은 무엇이다 I

public abstract class Employee implements Cloneable 
    { 
     private String firstName; 
     private String lastName; 
     private String socialSecurityNumber; 
     private Date_Of_Birth Date_Of_Birth_Inst; 

     // three-argument constructor 
     public Employee(String first, String last, String ssn, int Day, 
          int Month, int Year) 
     { 
      firstName = first; 
      lastName = last; 
      socialSecurityNumber = ssn; 
      Date_Of_Birth_Inst = new Date_Of_Birth(Day, Month, Year); 
     } 

     .... 
     Some Get and Set functions. 
     .... 

     @Override 
     protected Employee clone() throws CloneNotSupportedException { 
      Employee clone = (Employee) super.clone(); 
      return clone; 
     } 
    } 

이 여기에 서브 클래스

public class Employee_Subclass extends Employee{ 

    public Employee_Subclass(String first, String last, String ssn, int Day, 
         int Month, int Year) 
    { 
      super(first, last, ssn, Day, Month, Year); 
     } 

} 

만 생성자입니다.

다음은 기본 파일입니다.

public class Test_Project { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws CloneNotSupportedException { 

     Employee_Subclass Employee_Inst = new Employee_Subclass("Hello", "World", 
                "066499402", 7, 6, 1984); 

     Employee_Subclass Employee_Inst1; 

     Employee_Inst1 = (Employee_Subclass) Employee_Inst.clone(); 
    }   

} 

throws CloneNotSupportedException을 입력해야합니다. 그렇지 않으면 작동하지 않습니다.

제 질문은 정확히 어떻게 작동합니까?

내가) Employee_Inst.clone (전화, 그것은 바로, 직원의 클론 함수를 호출?

지금,이 기능은 직원의 객체를 반환, 그래서 그것은 서브 클래스 객체로 어떻게 삽입 할 수 있습니다?

깊은 복제품에 대해서는 올바르게 했습니까? Date_Of_Birth_Inst는 올바르게 복사 되었습니까?

고마워요.

답변

2

Employee 클래스를 Cloneable으로 구현하는 것을 잊어 버린 것입니다.

+0

나는 ... 감사합니다 난 그냥 그렇게하고 싶어 :) 감사 – user2102697

0

당신은 내가 대신 Cloneable 인터페이스를 사용하는 복사 생성자를 구현 제안하여 Employee 클래스

0

implement Cloneable해야합니다. 자세한 내용은 Joshua J. Bloch explanation을 참조하십시오.

+0

을 몰랐 잊지 않았다, 그러나 나의 강사에 따라 수 없습니다. – user2102697

0

Cloneable 인터페이스를 구현해야합니다. 다음 예는 이해와 딥 복제 및 얕은 복제에 도움이 될 수 있습니다.

import java.util.ArrayList; 
import java.util.List; 

public class DeepCopy implements Cloneable { 
    private List<String> hobbiesList; 
    private int age; 
    private String name; 
    private float salary; 

    public static void main(String[] args) throws CloneNotSupportedException { 
     DeepCopy original = new DeepCopy(); 
     original.name = "AAA"; 
     original.age = 20; 
     original.salary = 10000; 
     original.hobbiesList = new ArrayList<String>(); 
     original.hobbiesList.add("Cricket"); 
     original.hobbiesList.add("Movies"); 
     original.hobbiesList.add("Guitar"); 
     original.hobbiesList.add("Eating"); 

     DeepCopy cloned = (DeepCopy) original.clone(); 
     System.out.println("original:=" + original); 
     System.out.println("cloned :=" + cloned); 
     System.out.println("After adding two more hobbies in 'original' which untimately reflected in 'cloned'"); 
     cloned.name = "BBB"; 
     cloned.age = 27; 
     cloned.salary = 18237; 
     cloned.hobbiesList.add("Trecking"); 
     System.out.println("original  :=" + original); 
     System.out.println("cloned changed:=" + cloned); 
    } 

    @Override 
    protected DeepCopy clone() throws CloneNotSupportedException { 
     DeepCopy clone = (DeepCopy) super.clone(); 
     clone.hobbiesList = new ArrayList<String>(clone.hobbiesList); 
     return clone; 
    } 

    @Override 
    public String toString() { 
     return "My name is (String)" + name + " having age (int)" + age + ". I earned (float)" + salary + " and hobbies are (ArrayList)" + hobbiesList; 
    } 
} 
관련 문제