大话设计模式-原型模式
项目经验类:
/**
* 项目经验类
*/
public class ProjectExperience implements Cloneable {
// 项目名称及项目语言
private String name;
private String lanuage;
public ProjectExperience() {
}
public ProjectExperience(String name, String lanuage) {
this.name = name;
this.lanuage = lanuage;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "项目经验:{" +
"name='" + name + '\'' +
", lanuage='" + lanuage + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLanuage() {
return lanuage;
}
public void setLanuage(String lanuage) {
this.lanuage = lanuage;
}
}
简历类:
/**
* 简历
*/
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;
// 项目经验
private ProjectExperience projectExperience;
public Resume() {
}
public Resume(String name, String sex, String age, String timeArea, String company, ProjectExperience projectExperience) {
this.name = name;
this.sex = sex;
this.age = age;
this.timeArea = timeArea;
this.company = company;
this.projectExperience = projectExperience;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Resume obj = new Resume();
obj.name = this.name;
obj.age = this.age;
obj.sex = this.sex;
obj.projectExperience = (ProjectExperience) this.projectExperience.clone();
return obj;
}
/**
* 设置个人信息
*
* @param sex
* @param age
*/
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
/**
* 设置工作经历
*
* @param timeArea
* @param company
*/
public void setWorkExperience(String timeArea, String company) {
this.timeArea = timeArea;
this.company = company;
}
/**
* 设置项目经历
*
* @param name
* @param lanuage
*/
public void setProjectExperience(String name, String lanuage) {
projectExperience.setName(name);
projectExperience.setLanuage(lanuage);
}
@Override
public String toString() {
return "基本信息:{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
", timeArea='" + timeArea + '\'' +
", company='" + company + '\'' +
'}'
+ "\n" + projectExperience.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTimeArea() {
return timeArea;
}
public void setTimeArea(String timeArea) {
this.timeArea = timeArea;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
Main:
public class Main {
public static void main(String[] args) {
ProjectExperience projectExperience = new ProjectExperience("Android-QQ", "Java");
Resume a = new Resume("大鸟", "男", "29", "1998-2000", "Tecent", projectExperience);
System.out.println(a.toString());
try {
Resume b = (Resume) a.clone();
b.setWorkExperience("2000-2008", "BaiDu");
b.setProjectExperience("Deepfakes-换脸", "C++ & Python");
System.out.println(b.toString());
System.out.println(a.toString());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}