問題描述:eclipse的版本是oxygen的,JDK和jre的版本是10;在eclipse中使用clone()方法進行淺克隆的時候,出現(xiàn)如下提示的編譯錯誤。提示“返回類型與Object.clone()不兼容”,具體位置如下圖所示:但是我在使用JDK編譯運行的時候是正常的,所以基本上能確定是eclipse哪里出現(xiàn)問題,所以想請問各位大佬這個是什么原因造成的問題(我猜測是eclipse的版本和JDK的版本不兼容的問題,但是不敢確定)源碼附上:public?class?Address?{
????????private?String?country;
????????private?String?province;
????????private?String?city;
????????
????????public?void?setCountry(String?country)?{
????????????this.country=country;
????????}
????????
????????public?void?setProvince(String?province)?{
????????????this.province=province;
????????}
????????
????????public?void?setCity(String?city)?{
????????????this.city=city;
????????}
????????
????????public?String?toString()?{
????????????//return?"地址:"+"國家:"+country+","+"省:"+province+","+"市:"+city;
????????????
????????????StringBuilder?sb=new?StringBuilder();
????????????sb.append("國家:"+country+",");
????????????sb.append("省:"+province+",");
????????????sb.append("市:"+city);
????????????return?sb.toString();
????????}
????
}public?class?Employee?implements?Cloneable{???
????????private?String?name;
????????private?int?age;
????????private?Address?address;
????
????????public?void?setName(String?name){
????????????this.name=name;
????????}
????
????????public?void?setAge(int?age){
????????????this.age=age;
????????}
????
????????public?void?setAddress(Address?address)?{
????????????this.address=address;
????????}
????????
?????????public?Address?getAddress(){
???????????????return?this.address;
????????}
????
????????public?Employee?clone()?{
????????????Employee?employee=null;
????????????try?{
????????????????employee=(Employee)super.clone();
????????????}catch(CloneNotSupportedException?e)?{
????????????????e.printStackTrace();
????????????}
????????
????????????return?employee;
????????}
????
????
????????public?String?toString()?{?????????????????????//重寫toString方法,使用字符串輸出對象
????????????StringBuilder?sb=new?StringBuilder();
????????????sb.append("姓名:"+name+",");
????????????sb.append("年齡:"+age+'\n');
????????????sb.append(address);
????????????return?sb.toString();
????????}
}public?class?Test?{
????public?static?void?main(String[]?args)?{
????????System.out.println("克隆之前:");
????????Address?address=new?Address();
????????address.setCountry("中國");
????????address.setProvince("四川");
????????address.setCity("成都");
????????Employee?employee1=new?Employee();
????????employee1.setName("張XX");
????????employee1.setAge(30);
????????employee1.setAddress(address);
????????System.out.println("員工1的信息");
????????System.out.println(employee1);
????????
????????System.out.println("克隆之后:");
????????Employee?employee2=employee1.clone();??//使用淺克隆
????????????????????????
????????employee2.setName("陳XX");????????????
????????employee2.setAge(20);
????????employee2.getAddress().setCountry("中國");????????????
????????employee2.getAddress().setProvince("山東");
????????employee2.getAddress().setCity("濟南");
????????
????????
????????System.out.println("員工1的信息");
????????System.out.println(employee1);
????????System.out.println("員工2的信息");
????????System.out.println(employee2);
????}
}
關于eclipse中使用clone()方法的問題
慕碼人3756810
2018-03-28 23:30:57