========================================================================= ================================= 추상 슈퍼 클래스의 클론을 오버라이드
그래서 나와 함께 곰하시기 바랍니다 자바에 새로운 조금 해요 :)나는 수퍼 클래스를 만들었습니다.
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는 올바르게 복사 되었습니까?
고마워요.
나는 ... 감사합니다 난 그냥 그렇게하고 싶어 :) 감사 – user2102697