




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)6抽象類和接口一、 實(shí)驗(yàn)?zāi)康模?、 學(xué)習(xí)掌握抽象類的概念和使用方法。2、 學(xué)習(xí)掌握接口的概念和定義接口的方法。3、 學(xué)習(xí)使用Cloneable接口和clone方法進(jìn)行對(duì)象內(nèi)容的復(fù)制。4、 理解淺復(fù)制和深復(fù)制的概念,掌握覆蓋clone方法進(jìn)行對(duì)象內(nèi)容深復(fù)制的技術(shù)。二、 實(shí)驗(yàn)任務(wù):1、 學(xué)習(xí)掌握抽象類的概念和使用方法。程序要求:(1) 首先創(chuàng)建一個(gè)類家族,其中抽象類幾何圖形類GeometricObject為父類,圓類Circle和矩形類Rectangle為子類。幾何圖形類GeometricObject中定義保護(hù)型字符串變量color,表示圖形的顏色;該類要具備構(gòu)造方法和兩個(gè)抽象方法findAr
2、ea和findPerimeter,抽象方法findArea求圖形面積,抽象方法findPerimeter求圖形周長(zhǎng)。(2) Circle類和Rectangle類是GeometricObject類的子類,其中應(yīng)實(shí)現(xiàn)父類的抽象方法。(3) 程序主方法中創(chuàng)建兩個(gè)幾何對(duì)象,一個(gè)圓和一個(gè)矩形,并用GeometricObject類的引用變量引用它們,調(diào)用抽象方法。 2、 學(xué)習(xí)接口的概念和利用接口實(shí)現(xiàn)多態(tài)的方法。程序要求如下:(1) 首先創(chuàng)建圓類Circle和圓柱體類Cylinder,其中Circle類是父類,Cylinder類是子類;(2) 創(chuàng)建接口Comparable,其中包含一個(gè)抽象方法compare
3、To,用來(lái)比較對(duì)象的大小。抽象方法compareTo的形式如下: public int compareTo(Object o);(3) 創(chuàng)建類ComparableCircle,該類為Circle類的子類,并實(shí)現(xiàn)Comparable接口。(4) 創(chuàng)建類ComparableCylinder,該類為Cylinder類的子類,并實(shí)現(xiàn)Comparable接口。(5) 創(chuàng)建通用類Max,其中包含通用方法max,只要類實(shí)現(xiàn)了Comparable接口,就可以使用max方法返回兩個(gè)對(duì)象中較大的一個(gè)。Max方法的方法名稱為: public static Comparable max(Comparable o1,
4、Comparable o2)(6) 程序的主方法中分別創(chuàng)建兩個(gè)ComparableCircle類對(duì)象和兩個(gè)ComparableCylinder類對(duì)象,并分別以它們?yōu)閰?shù)調(diào)用max方法,返回兩個(gè)對(duì)象中面積較大的一個(gè)。3、 選做題:進(jìn)一步深入學(xué)習(xí)多態(tài)特性以及利用Cloneable接口和clone方法實(shí)現(xiàn)對(duì)象內(nèi)容的拷貝,并學(xué)習(xí)消除淺拷貝(淺復(fù)制)的方法。程序要求如下:(1) 創(chuàng)建Circle類,表示圓;(2) 創(chuàng)建Name類,表示人名,其中包含三個(gè)String類型的數(shù)據(jù)成員:firstName,middlName和lastName。(3) 創(chuàng)建CloneableCircle類,CloneableCi
5、rcle類是Circle類的子類,并實(shí)現(xiàn)了Cloneable接口。要求CloneableCircle類中有一個(gè)Name類型的數(shù)據(jù)成員creator,代表圓對(duì)象的創(chuàng)建者姓名。(4) 在CloneableCircle類中實(shí)現(xiàn)clone方法,以實(shí)現(xiàn)兩個(gè)CloneableCircle類對(duì)象內(nèi)容的克隆。要求實(shí)現(xiàn)對(duì)象內(nèi)容深拷貝(深復(fù)制)。(5) 為了實(shí)現(xiàn)CloneableCircle類對(duì)象的深拷貝,Name類也應(yīng)該實(shí)現(xiàn)Cloneable接口,并實(shí)現(xiàn)clone方法。(6) 程序的主方法中使用clone方法完成兩個(gè)CloneableCircle類對(duì)象的深拷貝。三、 實(shí)驗(yàn)步驟:1.參考代碼:abstract c
6、lass GeometricObject protected String color; protected double weight; protected GeometricObject(String color, double weight) this.color = color; this.weight = weight; public abstract double findArea(); public abstract double findPerimeter();class Circle extends GeometricObject protected double radiu
7、s; public Circle(double radius) super("white", 1.0); this.radius = radius; public double findArea() return radius*radius*Math.PI; public double findPerimeter() return 2*radius*Math.PI; class Rectangle extends GeometricObject protected double width; protected double height; public Rectangle
8、(double width, double height) super("white", 1.0); this.width = width; this.height = height; public double findArea() return width*height; public double findPerimeter() return 2*(width + height); public class TestAb public static void main(String args) GeometricObject a1 = new Circle(5); G
9、eometricObject a2= new Rectangle(5, 3); test(a1); test(a2); public static void test(GeometricObject a) System.out.println(a.findArea(); 2.參考代碼:/Circle類的實(shí)現(xiàn)class Circle protected double radius; public Circle() radius=1.0; public Circle(double r) radius=r; double getRadius()return radius; public double
10、 findArea() return radius*radius*Math.PI; / Cylinder的實(shí)現(xiàn)class Cylinder extends Circle private double length; public Cylinder() super(); length = 1.0; public Cylinder(double r, double l) super(r); length = l; public double findArea() return 2*super.findArea()+(2*getRadius()*Math.PI)*length; public dou
11、ble findVolume() return super.findArea()*length; / ComparableCircle類的實(shí)現(xiàn)class ComparableCircle extends Circle implements Comparable public ComparableCircle(double r) super(r); public int compareTo(Object o) if (findArea() > (Circle)o).findArea() return 1; else if (findArea() < (Circle)o).findAr
12、ea() return -1; else return 0; / ComparableCylinder類的實(shí)現(xiàn)class ComparableCylinder extends Cylinder implements Comparable ComparableCylinder(double r, double l) super(r, l); public int compareTo(Object o) if (findVolume() > (Cylinder)o).findVolume() return 1; else if (findVolume() < (Cylinder)o).
13、findVolume() return -1; else return 0; /Max類實(shí)現(xiàn) class Max public static Comparable max(Comparable o1, Comparable o2) if (pareTo(o2) > 0) return o1; else return o2; /主函數(shù)類的實(shí)現(xiàn)public class TestInterface public static void main(String args) ComparableCircle circle1 = new ComparableCircle(5); ComparableCircle circle2 = new ComparableCircle(4); Comparable circle = Max.max(circle1, circle2); System.out.println("最大圓半徑為: " + (Circle)circle).getRadius(); ComparableCylinder cylinder1 = new ComparableCylinder(5, 2); ComparableCylinder cylinder2 = new Compar
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)級(jí)數(shù)據(jù)共享的區(qū)塊鏈技術(shù)發(fā)展趨勢(shì)預(yù)測(cè)
- 嚴(yán)控廢物合同范例
- 豐澤區(qū) 勞動(dòng)合同樣本
- 醫(yī)療數(shù)據(jù)安全與隱私保護(hù)的國(guó)際法規(guī)探討
- 批評(píng)與自我批評(píng)發(fā)言稿模版
- 綠橙色可愛卡通食品安全模板
- 15天賬期合同范例
- 從商業(yè)視角看如何利用區(qū)塊鏈技術(shù)增強(qiáng)數(shù)據(jù)安全性
- 員工禮儀學(xué)習(xí)心得體會(huì)模版
- 廈門市2025 屆高三畢業(yè)班第四次質(zhì)量檢測(cè)-化學(xué)+答案
- 2024年煙臺(tái)龍口市衛(wèi)生健康局所屬事業(yè)單位招聘工作人員真題
- 二零二五版官方離婚協(xié)議書
- 《念奴嬌++過洞庭》教學(xué)設(shè)計(jì)++2024-2025學(xué)年統(tǒng)編版高一語(yǔ)文必修下冊(cè)
- 四川省綿陽(yáng)市高中2022級(jí)第三次診斷性考試地理試題及答案(A卷)
- 交通樞紐的安全管理事故預(yù)防與應(yīng)急處理策略
- 上海市靜安區(qū)2025年高三二模英語(yǔ)試卷(含答案)
- 護(hù)理管理學(xué)中的化學(xué)性傷害
- 輔導(dǎo)員考試基礎(chǔ)知識(shí)考題盤點(diǎn)
- 閥門技術(shù)協(xié)議合同
- 2025江蘇省安全員B證考試題庫(kù)
- 客戶旅程全維度管理研究-全面剖析
評(píng)論
0/150
提交評(píng)論