package object_method;
public abstract class Student implements Cloneable{
private int age;
private String name;
public String profession="学生";
public Student(){}
public Student(int age,String name){
this.age=age;
this.name=name;
}
public void show(){
System.out.println("姓名:"+name+" 年龄:"+age+" 职业:"+profession);
}
public abstract void learn();
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + ", profession="
+ profession + "]";
}
}
package object_method;
public class HighSchoolStudent extends Student{
public HighSchoolStudent(){}
public HighSchoolStudent(int age,String name){
super(age,name);
}
@Override
public void learn() {
// TODO Auto-generated method stub
System.out.println("高中生学基础知识");
}
}
package object_method;
public class StudentDemo {
/**
* @param args
* @throws CloneNotSupportedException
* @throws Throwable
*/
public static void main(String[] args) throws CloneNotSupportedException{
// TODO Auto-generated method stub
Student s=new HighSchoolStudent(16,"Tony");
System.out.println("被克隆学生s:");
s.show();
s.learn();
System.out.println(s.toString());
System.out.println("哈希码值:"+s.hashCode());
System.out.println("--------------------");
System.out.println("现在开始克隆");
System.out.println("--------------------");
Object obj=s.clone();
Student s2=(Student)obj;
System.out.println("克隆学生s2:");
s2.show();
s2.learn();
System.out.println("哈希码值:"+s2.hashCode());
System.out.println("-------------------");
System.out.println("学生s的镜像:");
Student s3=new HighSchoolStudent();
System.out.println("哈希码值:"+s3.hashCode());
s3=s;
s3.show();
s3.learn();
System.out.println("哈希码值:"+s3.hashCode());
System.out.println("-------------------");
System.out.println("s2是s:"+s.equals(s2));
System.out.println("s3是s:"+s.equals(s3));
System.out.println("-------------------");
Student s4=new HighSchoolStudent();
System.out.println("哈希码值:"+s4.hashCode());
}
}
运行后输出如下
被克隆学生s:
姓名:Tony 年龄:16 职业:学生
高中生学基础知识
Student [age=16, name=Tony, profession=学生]
哈希码值:23668144
--------------------
现在开始克隆
--------------------
克隆学生s2:
姓名:Tony 年龄:16 职业:学生
高中生学基础知识
哈希码值:2719739
-------------------
学生s的镜像:
哈希码值:9523050
姓名:Tony 年龄:16 职业:学生
高中生学基础知识
哈希码值:23668144
-------------------
s2是s:false
s3是s:true
-------------------
哈希码值:32820206
|
|