import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Setter
@Getter
@Slf4j
public class Studentextends BaseDtoimplements Serializable, Cloneable{
private Integerid;
private Stringname;
private Integerage;
private Citycity;
private List
public Objectclone() {
//浅拷贝
try {
// 直接调用父类的clone()方法
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
public static void main(String[] args) throws CloneNotSupportedException{
Student s1 =new Student();
s1.setAge(10);
s1.setId(1);
s1.setName("1111111111111111");
s1.getList().add(1);
City city =new City();
city.setCityCode(55555);
city.setName("Unkown");
s1.setCity(city);
log.info("s1 is:{}", JSON.toJSONString(s1));
String content = JSON.toJSONString(s1);
Student s11 = JSON.parseObject(content, Student.class);
s11.setId(11);
s11.setAge(11);
s11.setName("000000000000000");
s11.getList().add(11);
log.info("s11 is:{}", JSON.toJSONString(s11));
log.info("s1 is:{}", JSON.toJSONString(s1));
content = JSON.toJSONString(s1);
Student s2 =(Student) s1.clone();
s2.setId(2);
s2.setAge(22);
s2.setName("22222222222222");
s2.getList().add(2);
s2.getCity().setName("Beijing");
log.info("s2 is:{}", JSON.toJSONString(s2));
log.info("s1 is:{}", JSON.toJSONString(s1));
content = JSON.toJSONString(s1);
Student s3 = JSON.parseObject(content, Student.class);
s3.setId(3);
s3.setAge(33);
s3.setName("333333333333");
s3.getList().add(3);
log.info("s3 is:{}", JSON.toJSONString(s3));
Student s4 =(Student) s1.clone();
s4.setId(4);
s4.setAge(44);
s4.setName("44444444444444");
s4.getList().add(4);
log.info("s4 is:{}", JSON.toJSONString(s4));
log.info("s2 is:{}", JSON.toJSONString(s2));
log.info("s1 is:{}", JSON.toJSONString(s1));
}
@Data
public class City{
private IntegercityCode;
private Stringname;
}
}