java技术博客

jsp博客
数据加载中……
CloneTest.java

 

/**
*i测试对象的克隆及clone方法的重写
*/

public class CloneTest{
public static void main(String[] args){
Student tom
=new Student("tom","20020410");
Student tomcopy
=(Student)tom.clone();
tomcopy.setStudentSex(
"man");
tomcopy.setStudentAddress(
"America");
System.out.println(tom);
System.out.println(tomcopy);
}

}

/*
 * 学生类,包括学生的基本信息,实现了Cloneable接口
 
*/

class Student implements Cloneable
{
 
private String strName = "";//学生姓名
 private String strNumber = "";//学号
 private String strSex = "";//性别
 private String strBirthday = "";//出生年月
 private String strSpeciality = "";//专业
 private String strAddress = "";//地址
 
 
public Student(String name, String number)
 
{
  strName 
= name;
  strNumber 
= number;
 }

public Object clone(){
try{
return super.clone();
}

catch(CloneNotSupportedException e){
return null;}

}

public String getStudentName(){
return strNumber;
}

public void setStudentSex(String sex)
{
strSex
=sex;
}

public String getStudentSex(){
return strSex;
}

public String getStudentBirthday(){
return strBirthday;
}

public String getStudentSpeciality(){
return strSpeciality;
}

public void setStudentSpeciality(String speciality){
strSpeciality
=speciality;
}

public String getStudentAddress() {
return strAddress;
}

public void setStudentAddress(String address){
strAddress
=address;
}

public Stirng toString()
{
String information
="student name="+strName+",student number="+strNumber;
if(!strSex.equals(""))
information
+=",sex="+strSex;
if(!strBirthday.equals(""))
information
+=",birthday="+strBirthday;
if(!strSpeciality.equals(""))
information
+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information
+=",address="+strAddress;
return information;
}

}

}

posted on 2008-10-23 14:16 郭兴华 阅读(146) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: