Customer类的设计

/*
 * Customer为实体对象,用来封装客户信息
 */
public class Customer {
	private String name;//客户姓名
	private char gender;//性别
	private int age;//年龄
	private String phone;//电话号码
	private String email;//电子邮箱
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public char getGender() {
		return gender;
	}
	
	public void setGender(char gender) {
		this.gender = gender;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getPhone() {
		return phone;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

public Customer() {
}

public Customer(String name, char gender, int age, String phone, String email) {

	this.name = name;
	this.gender = gender;
	this.age = age;
	this.phone = phone;
	this.email = email;
}



}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java语言设计的客户Customer及其关联的地址Address的完整实现。其中,浅克隆方式只克隆了地址对象的引用,而深克隆方式则复制了一个新的地址对象。 ```java // Address public class Address implements Cloneable { private String street; private String city; private String state; private String zip; public Address(String street, String city, String state, String zip) { this.street = street; this.city = city; this.state = state; this.zip = zip; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } @Override public Address clone() throws CloneNotSupportedException { return (Address) super.clone(); } } // Customer public class Customer implements Cloneable { private String name; private Address address; public Customer(String name, Address address) { this.name = name; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } // 浅克隆 @Override public Customer clone() throws CloneNotSupportedException { return (Customer) super.clone(); } // 深克隆 public Customer deepClone() throws CloneNotSupportedException { Customer cloned = (Customer) super.clone(); cloned.setAddress(cloned.getAddress().clone()); return cloned; } } ``` 对于浅克隆和深克隆的比较,我们可以编写以下测试代码: ```java public class Test { public static void main(String[] args) throws CloneNotSupportedException { Address address = new Address("123 Main St.", "Anytown", "CA", "12345"); Customer customer1 = new Customer("John Doe", address); // 浅克隆 Customer customer2 = customer1.clone(); System.out.println(customer1 == customer2); // false System.out.println(customer1.getAddress() == customer2.getAddress()); // true // 深克隆 Customer customer3 = customer1.deepClone(); System.out.println(customer1 == customer3); // false System.out.println(customer1.getAddress() == customer3.getAddress()); // false } } ``` 在浅克隆方式下,克隆出来的Customer对象和原对象不是同一个对象,但是它们所引用的Address对象是同一个对象。而在深克隆方式下,克隆出来的Customer对象和原对象也不是同一个对象,但是它们所引用的Address对象却不是同一个对象。由此可以看出,深克隆方式比浅克隆方式更加安全,但是也更加耗费资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值