2016級java語言實(shí)驗(yàn)3指導(dǎo)(面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài)))(共14頁)_第1頁
2016級java語言實(shí)驗(yàn)3指導(dǎo)(面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài)))(共14頁)_第2頁
2016級java語言實(shí)驗(yàn)3指導(dǎo)(面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài)))(共14頁)_第3頁
2016級java語言實(shí)驗(yàn)3指導(dǎo)(面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài)))(共14頁)_第4頁
2016級java語言實(shí)驗(yàn)3指導(dǎo)(面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài)))(共14頁)_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上上機(jī)實(shí)驗(yàn)三:面向?qū)ο蟪绦蛟O(shè)計(jì)(繼承、封裝、多態(tài))類是面向?qū)ο蟪绦蛟O(shè)計(jì)的基礎(chǔ),是Java的核心和本質(zhì)所在,在Java中,所有的語言元素都封裝在類中。編寫java程序的過程就是從現(xiàn)實(shí)世界中抽象出java可實(shí)現(xiàn)的類,并用合適的語句定義它們的過程,本節(jié)將學(xué)習(xí)類的應(yīng)用,以及如何創(chuàng)建類的實(shí)例,通過類的繼承更有效的組織程序結(jié)構(gòu),明確類之間的關(guān)系。掌握本節(jié)所講的內(nèi)容后,讀者就可以使用面向?qū)ο蠹夹g(shù)編寫java程序了。接口是特殊的抽象類,只包含常量和方法的定義,而沒有方法的實(shí)現(xiàn),也就是說接口是方法定義和常量值的集合。包是Java語言中有效管理類的一個(gè)機(jī)制。通過關(guān)鍵字package聲明包語

2、句,package語句作為Java源文件的第一條語句,指明該源文件定義的類所在的包。使用import語句可以引入包中的類。一、實(shí)驗(yàn)?zāi)康?) 掌握類的定義和使用2) 掌握對象的聲明和使用3) 了解構(gòu)造函數(shù)的概念和使用4) 掌握類的繼承關(guān)系和派生方法5) 掌握多態(tài)的概念與使用6) 掌握接口的定義和使用7) 掌握J(rèn)ava中包的應(yīng)用二、實(shí)驗(yàn)內(nèi)容1) 類的聲明2) 定義類成員變量以及成員方法3) 實(shí)例化類、創(chuàng)建類對象以及類方法的調(diào)用4) 類的繼承5) 通過實(shí)例理解接口的定義6) 通過實(shí)例熟悉接口的應(yīng)用7) 正確應(yīng)用Java中包和import語句三、實(shí)驗(yàn)步驟1) 類和類的實(shí)例化一個(gè)類的實(shí)現(xiàn)包括兩部分:類聲

3、明和類體。(1)、類聲明publicabstractfinal class className extends superclassNameimplements interfaceNameList期中修飾符publicabstractfinal說明類的屬性className為類名superclassName為父類的名字interfaceNameList為類實(shí)現(xiàn)的接口列表(2)、類體類體定義如下class classNamepublic|protected|private static final transient volatileType variableName; /成員變量public|

4、protected|private static final abstract native synchronizedreturnType methondName ( paramList ) throws exceptionListstatements /成員方法(3)、成員變量成員變量的聲明方式如下 public|protected|private static final transient volatileType variableName; /成員變量其中:public|protected|private 可見性 static 靜態(tài)變量(類變量),相當(dāng)于實(shí)例變量 final 常量 tr

5、ansient 暫時(shí)性變量,用于對象存檔 volatile 變量,用于共發(fā)線程的共享(4)、成員方法public|protected|private static final abstract native synchronizedreturnType methondName ( paramList ) throws exceptionList/方法體的聲明statements /方法體其中:public|protected|private 可見性 static 靜態(tài)方法,也叫類方法,可以通過類名直接調(diào)用 final 方法不能被重寫abstract 抽象方法,沒有方法體(體現(xiàn)多態(tài)時(shí)常用) na

6、tive 集成其他語言的代碼 synchronized 控制多個(gè)并發(fā)線程的訪問例子3-1請根據(jù)注釋填寫語句,并上機(jī)調(diào)試成功根據(jù)注釋添加語句,并調(diào)試和修改程序,使其能夠執(zhí)行。-class Retangle public Retangle(double l, double w) length = l; width = w; public double calcPerimeter() /返回周長 (1) public double calcArea() /返回面積 (2) public void Show() /顯示矩形的長和寬 (3) protected double length; protec

7、ted double width; class Square extends Retangle public Square(double side) /調(diào)用父類的構(gòu)造方法 (4) public double calcPerimeter() return width * 4; public void Show() System.out.println( “邊長為” + width + “的正方形”); public class Test public static void main(String args)Square sq1=new Square(2.0); sq1.Show();Syste

8、m.out.println("正方形的邊長為"+sq1.calcPerimeter();(1) return (width+length)*2;(2) return width*length;(3) System.out.print("length="+length+"n"+"width="+width;(4) super(side,side);-填寫的語句是:class Retangle public Retangle(double l, double w) length = l; width = w; publ

9、ic double calcPerimeter() /返回周長 return (width+length)*2; public double calcArea() /返回面積 return width*length; public void Show() /顯示矩形的長和寬 System.out.print("length="+length+"n"+"width="+width ) ; protected double length; protected double width; class Square extends Retan

10、gle public Square(double side) /調(diào)用父類的構(gòu)造方法 super(side,side); public double calcPerimeter() return width * 4; public void Show() System.out.println( "邊長為" + width + "的正方形"); public class Test public static void main(String args)Square sq1=new Square(2.0); sq1.Show();System.out.prin

11、tln("正方形的周長為"+sq1.calcPerimeter();2) 類的繼承繼承是面向?qū)ο蟪绦蛟O(shè)計(jì)的方法中的一種重要手段,通過繼承可以更有效的組織程序的結(jié)構(gòu),明確類之間的關(guān)系。繼承通過extends關(guān)鍵字來實(shí)現(xiàn),子類繼承父類的屬性和行為,同時(shí)可以修改父類的屬性和行為,并添加新的屬性和行為。Java不支持多重繼承。創(chuàng)建子類的格式如下class SubClass extends SuperClass其中 SubClass子類的名稱extends繼承的關(guān)鍵字SuperClass父類的名字 例子3-2 請分析程序,填寫語句的功能注釋,并上機(jī)調(diào)試成功。下面這個(gè)例子通過點(diǎn)、圓的層

12、次結(jié)構(gòu),介紹類的繼承。-import java.text.DecimalFormat; /調(diào)用格式化數(shù)字輸出import javax.swing.JOptionPane; / joptionpane讓您輕松彈出一個(gè)標(biāo)準(zhǔn)的對話框class Point /父類點(diǎn)的的定義 protected int x,y ; / 定義點(diǎn)的坐標(biāo)public Point() / 確定構(gòu)造函數(shù)setPoint(0,0); public Point(int a,int b) /構(gòu)造函數(shù)重載 帶參數(shù)的構(gòu)造函數(shù)setPoint(a,b);public void setPoint(int a,int b) / (1) x=a;

13、y=b;public int getX()return x; /獲得X的坐標(biāo)public int getY() return y; / 獲得Y的坐標(biāo)public String toString()return ""+x+","+"y"+""/*子類圓的定義*/class Circle extends Point /圓類繼承父類點(diǎn)類 protected double radius ;/定義圓的半徑public Circle() setRadius(0);/ 定義子類的構(gòu)造函數(shù),隱含調(diào)用了父類的構(gòu)造函數(shù)public C

14、ircle(double r,int a,int b)super(a,b);/ (2) setRadius(r);public void setRadius(double r)radius = (r>=0.0?r:0.0); / (3) public double getRadius() / 獲得圓半徑 return radius; public double area() / (4) return Math.PI*radius*radius; public String toString()/圓的半徑。以及圓心坐標(biāo)轉(zhuǎn)換成字符串輸出return "Center="+&

15、quot;"+x+","+y+""+"Radius="+radius; public class InheritanceTestpublic static void main(String args)Point pointRef,p;/聲明兩點(diǎn)對象Circle circleRef,c;/聲明兩圓對象String output;/定義一個(gè)字符串變量p=new Point(30,50);/給點(diǎn)對象賦值c=new Circle (2.7,120,89);/給圓對象賦值 /把點(diǎn)對象和圓對象轉(zhuǎn)換成字符串后給字符串output賦值out

16、put="Pointp:"+p.toString()+"nCirclec:"+c.toString();pointRef=c;output=output+"nnCircle c(via poineRef):"+pointRef.toString();circleRef=(Circle)pointRef; output=output+"nnCircle c(via poineRef):"+circleRef.toString();DecimalFormat precision2=new DecimalFormat(&

17、quot;0.00");output+= "nAreaofc(via circleRef):"+precision2.format(circleRef.area();/將圓定義成點(diǎn)對象輸出if( p instanceof Circle ) / (5) circleRef=(Circle) p;output+="nn cast successful" else output+="nn p dose not refer to a Circle"/利用對話框輸出相關(guān)信息JOptionPane.showMessageDialog(n

18、ull,output,"InheritanceTset",JOptionPane.INFORMATION_MESSAGE);/退出System.exit(0);語句的功能注釋:(1) 方法成員,設(shè)置變量x、y(2) 調(diào)用父類的構(gòu)造函數(shù) (3) 設(shè)置半徑 (4) 求取面積 2. 接口的定義與應(yīng)用接口聲明的形式如下所示:interface 接口名字 /常量定義和方法定義接口使用的關(guān)鍵字是implements,形式如下所示:class A implements Printable , Addable其中類A中使用接口Printable 和 Addable接口的特點(diǎn):1.類體中必須

19、實(shí)現(xiàn)接口中定義的所有方法;2. 接口中的方法和常量必須是public的。3.實(shí)現(xiàn)方法時(shí),方法頭必須一致(返回值類型,名字,參數(shù));4.父類被繼承時(shí),接口同時(shí)被繼承;5.接口也可被繼承,關(guān)鍵字為extends;6.接口一般表示功能,而類一般表示屬性。例子3-3本實(shí)例實(shí)現(xiàn)了一個(gè)字符棧。程序思路是:首先定義一個(gè)字符棧的接口CharStackInterface,定義了棧的空間容量,規(guī)定棧所包含的方法,然后定義棧類CharStack,該類實(shí)現(xiàn)了字符棧的接口,最后編寫測試類StackDemo進(jìn)行測試。請根據(jù)編程思路實(shí)現(xiàn)字符棧的接口CharStackInterface,并調(diào)試程序正確運(yùn)行,寫出程序執(zhí)行結(jié)果。

20、-interface CharStackinterface/需要實(shí)現(xiàn)的字符棧接口class CharStack implements CharStackinterfacechar data;int top;CharStack()data=new charmaxsize;public void initStack()top=-1;public boolean push(char x)if(!full()data+top=x;return true;elsereturn false;public char pop()if(!empty()top-;return datatop+1;elseretu

21、rn '0'public boolean empty()return top=-1;public boolean full()return top=maxsize-1;public class StcckDemo public static void main(String args)CharStack s=new CharStack();s.initStack();s.push('A');s.push('B');System.out.println(s.pop();System.out.println(s.pop();字符棧的接口CharSta

22、ckInterface:程序的執(zhí)行結(jié)果:四、上機(jī)實(shí)踐1.編寫程序:在圓類的基礎(chǔ)上派生出了圓錐類,按照注釋填空,并調(diào)試執(zhí)行成功,寫出程序的執(zhí)行結(jié)果。 class Circle double r ; Circle( ) (1) /無參構(gòu)造方法,默認(rèn)半徑為1.0Circle(double a) /有參構(gòu)造方法 (2) double Area( ) (3) /返回面積double Girth( ) (4) /返回周長class Cone extends Circledouble h;Cone() super();h=1.0;Cone(double rr,double hh) (5) /為半徑和高賦值

23、double V() (6) /返回圓錐的體積public class ConeTestpublic static void main(String args) Cone c1=new Cone(); System.out.println(c1.V(); Cone c2=new Cone(1.0,3.0); System.out.println(c2.V(); -按照注釋的填空:(1) r=1.0(2) this.a=a;(3) return Area(4) return Girth(5) rr=r; hh=h;(6) return V程序的執(zhí)行結(jié)果:c1./3c2. 2. 下面程序?qū)崿F(xiàn)了抽象

24、類的繼承和接口的實(shí)現(xiàn),同時(shí)還體現(xiàn)了接口的回調(diào)和向上轉(zhuǎn)型,請調(diào)試該程序,根據(jù)注釋填空,并寫出程序執(zhí)行結(jié)果。-abstract class Person public abstract void Eat();public abstract void Sleep();interface Father public void Smoking();public void GoFishing();interface Mon public void WatchTV(); public void Cook();class Me /(1)繼承Person類,實(shí)現(xiàn)Father和Mon接口 public void

25、Eat() System.out.println("我喜歡吃魚香茄子"); public void Sleep() System.out.println("我喜歡睡覺時(shí)做夢"); public void Smoking() System.out.println("我不喜歡抽煙"); public void GoFishing() System.out.println("我喜歡釣魚");public void WatchTV() System.out.println("我喜歡看電視");publi

26、c void Cook() System.out.println("我不太會做菜"); public class Test3 public static void main(String args) Person p1 = new Me(); p1.Eat(); /(2)調(diào)用p1的Sleep()方法 Mon m1 = (Mon)p1; /(3)調(diào)用m1的WatchTV()方法 m1.Cook(); -根據(jù)注釋填空:(1) extends Person implements Father,Mon(2) p1.Sleep(3) m1.Cook程序執(zhí)行結(jié)果:我喜歡吃魚香茄子我喜歡

27、睡覺時(shí)做夢媽媽喜歡看電視媽媽不太會做菜3. 將程序上機(jī)調(diào)試成功,并寫出程序執(zhí)行結(jié)果。-import java.util.*;interface Instrument5 int i = 5; void play(); String what(); void adjust();class Wind5 implements Instrument5 public void play() System.out.println("Wind5.play()"); public String what() return "Wind5" public void adjust() class Percussion5 implements Instrument5 public void play() System.out.println("Percussion5.play()"); public String what() return "Percussion5" public void

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論