java技術(shù)博客
jsp博客
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
數(shù)據(jù)加載中……
java的動態(tài)多態(tài)
/** */
/**
* 通過本程序的測試,主要學(xué)習(xí)抽象類及子類,抽象方法的實現(xiàn)
* 動態(tài)綁定,多態(tài)
*/
import
java.text.NumberFormat;
public
class
AbstractTest
{
public
static
void
main(String[] args)
{
Person[] p
=
new
Person[
2
];
p[
0
]
=
new
Worker(
"
Jack
"
,
1000
);
p[
1
]
=
new
Student(
"
Tom
"
,
"
Computer
"
);
for
(
int
i
=
0
; i
<
p.length; i
++
)
{
Person people
=
p[i];
System.out.println(people.getDescription());
}
}
}
/** */
/**
* 抽象類
*/
abstract
class
Person
{
private
String strName;
public
Person(String strName)
{
this
.strName
=
strName;
}
public
String getName()
{
return
strName;
}
//
抽象方法,返回人的描述
public
abstract
String getDescription();
}
/** */
/**
* 工人類,擴(kuò)展了抽象類,并實現(xiàn)了抽象方法
*/
class
Worker
extends
Person
{
private
double
salary;
public
Worker(String strName,
double
s)
{
super
(strName);
salary
=
s;
}
public
String getDescription()
{
NumberFormat formate
=
NumberFormat.getCurrencyInstance();
return
"
the worker with a salary of
"
+
formate.format(salary);
}
}
/** */
/**
* 學(xué)生類,擴(kuò)展了抽象類,實現(xiàn)了抽象方法
*/
class
Student
extends
Person
{
private
String strMajor;
public
Student(String strName, String strMajor)
{
super
(strName);
this
.strMajor
=
strMajor;
}
public
String getDescription()
{
return
"
the student majoring in
"
+
strMajor;
}
}
posted on 2008-11-07 16:31
郭興華
閱讀(280)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 郭興華
<
2008年11月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
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
1
2
3
4
5
6
統(tǒng)計
隨筆 - 84
文章 - 1
評論 - 2
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
java每日練習(xí)代碼
(rss)
TESTARRAY(6)
(rss)
事件模型與事件處理
(rss)
隨筆檔案
2009年1月 (2)
2008年11月 (14)
2008年10月 (68)
文章檔案
2008年10月 (1)
搜索
最新評論
1.?re: jsp讀取*.TXT
請問 retstr是什么數(shù)據(jù)類型?String?好像不行哦
--jsp
2.?re: StudentTest1.java
看不懂你的意思,代碼沒有縮進(jìn),看著很不習(xí)慣那。
--楊愛友
閱讀排行榜
1.?java中的treemap(4605)
2.?JDBC連接SQLSERVER(1822)
3.?判斷一個一個路徑是否是目錄(1085)
4.?jsp讀取*.TXT(765)
5.?java代理模式(727)
評論排行榜
1.?StudentTest1.java(1)
2.?jsp讀取*.TXT(1)
3.?java1.5注解(二)(0)
4.?java1.5注解(一)(0)
5.?jsp中使用類(0)
主站蜘蛛池模板:
赫章县
|
健康
|
易门县
|
湘西
|
丹巴县
|
靖江市
|
铁岭市
|
资中县
|
隆德县
|
哈密市
|
黄山市
|
荃湾区
|
鄱阳县
|
资兴市
|
辉县市
|
确山县
|
米易县
|
神池县
|
密云县
|
棋牌
|
青海省
|
北流市
|
枝江市
|
乾安县
|
隆尧县
|
巴南区
|
贡觉县
|
光山县
|
桐庐县
|
马关县
|
故城县
|
南部县
|
游戏
|
永州市
|
刚察县
|
循化
|
金秀
|
石楼县
|
会昌县
|
咸丰县
|
兴义市
|