问题描述:
eclipse的版本是oxygen的,JDK和jre的版本是10;
在eclipse中使用clone()方法进行浅克隆的时候,出现如下提示的编译错误。
提示“返回类型与Object.clone()不兼容”,具体位置如下图所示:
但是我在使用JDK编译运行的时候是正常的,所以基本上能确定是eclipse哪里出现问题,所以想请问各位大佬这个是什么原因造成的问题(我猜测是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); } }
卧龙绝圣
相关分类