java技術博客
jsp博客
BlogJava
首頁
新隨筆
聯系
聚合
管理
數據加載中……
CloneTest2.java
/**/
/*
*測試包含對象的克隆及clone方法的重寫
*/
import
java.util.GregorianCalendar;
import
java.util.Date;
public
class
CloneTest
{
public
static
void
main(String[] args)
{
Student tom
=
new
Student(
"
tom
"
,
"
20020410
"
);
tom.setEntryDate(
2002
,
01
,
02
);
Student tomcopy
=
(Student)tom.clone();
tomcopy.setStudentSex(
"
man
"
);
tomcopy.setEntryDate(
2003
,
10
,
23
);
System.out.println(tom.toString());
System.out.println(tomcopy.toString());
}
}
/**/
/*
* 學生類,包括學生的基本信息,實現了Cloneable接口
*/
class
Student
implements
Cloneable
{
private
String strName
=
""
;
//
學生姓名
private
String strNumber
=
""
;
//
學號
private
String strSex
=
""
;
//
性別
private
String strBirthday
=
""
;
//
出生年月
private
String strSpeciality
=
""
;
//
專業
private
String strAddress
=
""
;
//
地址
private
GregorianCalendar entryDate
=
new
GregorianCalendar();
//
入學日期
public
Student(String name, String number)
{
strName
=
name;
strNumber
=
number;
}
public
Object clone()
{
try
{
Student cloned
=
(Student)
super
.clone();
cloned.entryDate
=
(GregorianCalendar)entryDate.clone();
return
cloned;
}
catch
(CloneNotSupportedException e)
{
return
null
;
}
}
public
String getStudentName()
{
return
strName;
}
public
String getStudentNumber()
{
return
strNumber;
}
public
void
setStudentSex(String sex)
{
strSex
=
sex;
}
public
String getStudentSex()
{
return
strSex;
}
public
String getStudentBirthday()
{
return
strBirthday;
}
public
void
setStudentBirthday(String birthday)
{
strBirthday
=
birthday;
}
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
void
setEntryDate(
int
year,
int
month,
int
day)
{
entryDate.set(year, month
-
1
, day);
}
public
Date getEntryDate()
{
return
entryDate.getTime();
}
public
String getStudentName()
{
return
strName;
}
public
String getStudentNumber()
{
return
strNumber;
}
public
void
setStudentSex(String sex)
{
strSex
=
sex;
}
public
String getStudentSex()
{
return
strSex;
}
public
String getStudentBirthday()
{
return
strBirthday;
}
public
void
setStudentBirthday(String birthday)
{
strBirthday
=
birthday;
}
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
void
setEntryDate(
int
year,
int
month,
int
day)
{
entryDate.set(year, month
-
1
, day);
}
public
Date getEntryDate()
{
return
entryDate.getTime();
}
public
String toString()
{
String information
=
"
student name=
"
+
strName
+
"
,student number=
"
+
strNumber;
if
(
!
strSex.euquals(
""
))
information
+=
"
,birthday=
"
+
strBirthday;
if
(
!
strSpeciality.equals(
""
))
information
+=
"
,專業=
"
+
strSpeciality;
if
(
!
strAddress.equals(
""
))
information
+=
"
,address=
"
+
strAddress;
if
(entryDate
!=
null
)
information
+=
"
,entrydate=
"
+
getEntryDate();
return
information;
}
}
posted on 2008-10-23 14:40
郭興華
閱讀(146)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 郭興華
<
2008年10月
>
日
一
二
三
四
五
六
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
統計
隨筆 - 84
文章 - 1
評論 - 2
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
java每日練習代碼
(rss)
TESTARRAY(6)
(rss)
事件模型與事件處理
(rss)
隨筆檔案
2009年1月 (2)
2008年11月 (14)
2008年10月 (68)
文章檔案
2008年10月 (1)
搜索
最新評論
1.?re: jsp讀取*.TXT
請問 retstr是什么數據類型?String?好像不行哦
--jsp
2.?re: StudentTest1.java
看不懂你的意思,代碼沒有縮進,看著很不習慣那。
--楊愛友
閱讀排行榜
1.?java中的treemap(4604)
2.?JDBC連接SQLSERVER(1822)
3.?判斷一個一個路徑是否是目錄(1085)
4.?jsp讀取*.TXT(764)
5.?java代理模式(727)
評論排行榜
1.?StudentTest1.java(1)
2.?jsp讀取*.TXT(1)
3.?java1.5注解(二)(0)
4.?java1.5注解(一)(0)
5.?jsp中使用類(0)
主站蜘蛛池模板:
兴海县
|
盐山县
|
班戈县
|
正阳县
|
理塘县
|
百色市
|
玉树县
|
久治县
|
石林
|
潜山县
|
鹿邑县
|
梓潼县
|
大关县
|
德令哈市
|
武邑县
|
宁安市
|
松溪县
|
宁都县
|
金门县
|
东台市
|
镶黄旗
|
尼勒克县
|
大姚县
|
上林县
|
安达市
|
梁河县
|
青田县
|
民和
|
灵宝市
|
泰州市
|
蓝山县
|
荥经县
|
唐山市
|
济阳县
|
广南县
|
麻栗坡县
|
临猗县
|
东海县
|
鄢陵县
|
田林县
|
双牌县
|