Customer.java
package dgut.ke.javatest;
public class Customer implements Cloneable {
private String name;
private int age;
public Customer() {
this("unknown",0);
System.out.println("call default constructor");
}
public Customer(String name,int age) {
this.name = name;
this.age = age;
System.out.println("call second constructor");
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public boolean equals(Object o) {
if (this == o)
return true;
if (! (o instanceof Customer))
return false;
final Customer other = (Customer) o;
if (this.name.equals(other.name) && this.age == other.age)
return true;
else
return false;
}
public String toString() {
return "customer.name = "+name+" customer.age = "+age;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// 运用反射手段创建Customer对象
Class objectClass = Class.forName("dgut.ke.javatest.Customer");
Customer c1 = (Customer) objectClass.newInstance();
System.out.println("c1 --> " + c1);
//用new创建Customer对象
Customer c2 = new Customer("Tom",20);
System.out.println("c2 --> " + c2);
//运用克隆手段创建Customer对象
Customer c3 = (Customer) c2.clone();
System.out.println("c3 --> " + c3);
System.out.println("c2 == c3 " + (c2==c3));
System.out.println("c2.equals(c3) " + c2.equals(c3));
}
}