多態(tài)性和GUI資料(共23頁)_第1頁
多態(tài)性和GUI資料(共23頁)_第2頁
多態(tài)性和GUI資料(共23頁)_第3頁
多態(tài)性和GUI資料(共23頁)_第4頁
多態(tài)性和GUI資料(共23頁)_第5頁
已閱讀5頁,還剩19頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí) 驗(yàn) 報(bào) 告( 2015/ 2016 學(xué)年(xunin) 第1學(xué)期(xuq))課程名稱JAVA語言程序設(shè)計(jì)實(shí)驗(yàn)名稱多態(tài)性實(shí)驗(yàn)時(shí)間2015年 12 月 8日指導(dǎo)單位軟件工程系指導(dǎo)教師 學(xué)院(系)專 業(yè)學(xué)生姓名班級(jí)學(xué)號(hào) PAGE 22實(shí)驗(yàn)名稱多態(tài)性指導(dǎo)教師楊健實(shí)驗(yàn)類型驗(yàn)證實(shí)驗(yàn)學(xué)時(shí)2實(shí)驗(yàn)時(shí)間2015/12/8實(shí)驗(yàn)?zāi)康模?)、掌握多態(tài)性的實(shí)現(xiàn)。(2)、掌握抽象類和抽象方法的定義和使用。(3)、掌握接口的定義和實(shí)現(xiàn)。二、實(shí)驗(yàn)環(huán)境(實(shí)驗(yàn)設(shè)備)Eclipse實(shí)驗(yàn)內(nèi)容(1)、人員類的多態(tài)性設(shè)計(jì)。 設(shè)計(jì)一個(gè)人員類(Person),其中包含一個(gè)方法pay,代表人員的工資支出。再?gòu)腜erson類派生出助教類(As

2、sistant)、講師類(Instructor)、副教授類(AssistantProfesson)和教授類(Professon)。其中,工資支出=基本工資+授課時(shí)數(shù)每課時(shí)兼課金。但助教基本工資為800元,每課時(shí)兼課金25元,講師基本工資為1000元,每課時(shí)兼課金35元,副教授基本工資為1200元,每課時(shí)兼課金40元,教授基本工資1400元,每課時(shí)兼課金50元。 eq oac(,1)將Person定義為抽象類,pay為抽象方法,設(shè)計(jì)程序?qū)崿F(xiàn)多態(tài)性。 eq oac(,2)將Person定義為接口,設(shè)計(jì)程序?qū)崿F(xiàn)多態(tài)性。(2)利用applet畫出多個(gè)圖,實(shí)現(xiàn)圖形的多態(tài)性設(shè)計(jì)。 仔細(xì)閱讀下面的5個(gè)程序,

3、根據(jù)注解信息的提示,將程序中空缺的部分(由/*.*/標(biāo)注)補(bǔ)充完整,并上機(jī)調(diào)試正確,使程序運(yùn)行的結(jié)果是在applet的容器中畫出多種圖形。實(shí)驗(yàn)代碼: eq oac(,1)將Person定義為抽象類,pay為抽象方法,設(shè)計(jì)程序?qū)崿F(xiàn)多態(tài)性。package 多態(tài)性;public class aa public static void main(String args) Person ps=new Person4; ps0=new Assistant(張三,120); ps1=new Instructor(趙二,130); ps2=new AssistantProfesson(李四,125); ps3

4、=new Professor(王五,134); String output=; for(int i=0;ips.length;i+) output+=psi.toString();System.out.println(output); package 多態(tài)性;abstract class Person protected String name; protected double classsum; public Person() =; classsum=0; public Person(String name,double classsum) =name;

5、 this.classsum=classsum; public abstract double pay(); class Assistant extends Person public Assistant(String name,double classsum) super(name,classsum); public double pay() return 800+classsum*25; public String toString() return姓名:++t工資支出:+this.pay()+n; class Instructor extends Person publ

6、ic Instructor(String name,double classsum) super(name,classsum); public double pay() return 1000+classsum*35; public String toString() return姓名:++t工資支出:+this.pay()+n; class AssistantProfesson extends Person public AssistantProfesson(String name,double classsum) super(name,classsum); public

7、double pay() return 1200+classsum*40; public String toString() return姓名:++t工資支出:+this.pay()+n; class Professor extends Person public Professor(String name,double classsum) super(name,classsum); public double pay() return 1400+classsum*50; public String toString() return姓名:++t工資支出:+

8、this.pay()+n; eq oac(,2)將Person定義為接口,設(shè)計(jì)程序?qū)崿F(xiàn)多態(tài)性。package 多態(tài)性;public class aa public static void main(String args) Person ps=new Person4; ps0=new Assistant(張三,120); ps1=new Instructor(趙二,130); ps2=new AssistantProfesson(李四,125); ps3=new Professor(王五,134); String output=; for(int i=0;ips.length;i+) outp

9、ut+=psi.toString();System.out.println(output); package 多態(tài)性;interface Person public abstract double pay(); class Assistant implements Person protected String name; protected double classsum; public Assistant(String name,double classsum) =name; this.classsum=classsum; public double pay() retu

10、rn 800+classsum*25; public String toString() return姓名:++t工資支出:+this.pay()+n; class Instructor implements Person protected String name; protected double classsum; public Instructor(String name,double classsum) =name; this.classsum=classsum; public double pay() return 1000+classsum*3

11、5; public String toString() return姓名:++t工資支出:+this.pay()+n; class AssistantProfesson implements Person protected String name; protected double classsum; public AssistantProfesson(String name,double classsum) =name; this.classsum=classsum; public double pay() return 1200+classsum*40

12、; public String toString() return姓名:++t工資支出:+this.pay()+n; class Professor implements Person protected String name; protected double classsum; public Professor(String name,double classsum) =name; this.classsum=classsum; public double pay() return 1400+classsum*50; public String toS

13、tring() return姓名:++t工資支出:+this.pay()+n; :MyShape接口:package 多態(tài)性1;import java.awt.Graphics;public interface MyShape / int X1,Y1,X2,Y2;/method to draw the shape/* Declare header for method draw */void draw(Graphics g); /set and get methods for each of the four private variablesvoid setX1(int x

14、1);void setX2(int x2);void setY1(int y1);void setY2(int y2);int getX1();int getX2();int getY1();int getY2();/ end interface MyShapeMyLine.java:package 多態(tài)性1;import java.awt.Graphics;/MyLine.java/Declaration of class MyLinepublic class MyLine extends Object implements MyShapeprivate int X1,X2,Y1,Y2;pu

15、blic MyLine(int x1,int y1,int x2,int y2) / TODO Auto-generated constructor stubsetX1(x1);setY1(y1);setX2(x2);setY2(y2);Overridepublic void draw(Graphics g) / TODO Auto-generated method stubg.drawLine(getX1(),getY1(),getX2(),getY2();Overridepublic void setX1(int x1) / TODO Auto-generated method stubX

16、1=x1;Overridepublic void setX2(int x2) / TODO Auto-generated method stubX2=x2;Overridepublic void setY1(int y1) / TODO Auto-generated method stubY1=y1;Overridepublic void setY2(int y2) / TODO Auto-generated method stubY2=y2;Overridepublic int getX1() / TODO Auto-generated method stubreturn X1;Overri

17、depublic int getX2() / TODO Auto-generated method stubreturn X2;Overridepublic int getY1() / TODO Auto-generated method stubreturn Y1;Overridepublic int getY2() / TODO Auto-generated method stubreturn Y2;/end class MyLineMyOval.java:package 多態(tài)性1;import java.awt.Graphics;import java.awt.Graphics2D;im

18、port java.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;public class MyOval extends Object implements MyShape private int leftX, topY, width,height;public MyOval(int x1,int y1,int x2,int y2) / TODO Auto-generated constructor stubsetX1(x1);setY1(y1);setX2(x2);setY2(y2);Overridepublic void draw(

19、Graphics g) / TODO Auto-generated method stubGraphics2D g2 = (Graphics2D) g;/ 畫矩形Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);Ellipse2D ellipse = new Ellipse2D.Double();ellipse.setFrame(rect);g2.draw(ellipse);Overridepublic void setX1(int x1) / TODO Auto-generated method stu

20、bleftX=x1;Overridepublic void setX2(int x2) / TODO Auto-generated method stubwidth=x2;Overridepublic void setY1(int y1) / TODO Auto-generated method stubtopY=y1;Overridepublic void setY2(int y2) / TODO Auto-generated method stubheight=y2;Overridepublic int getX1() / TODO Auto-generated method stubre

21、turn leftX;Overridepublic int getX2() / TODO Auto-generated method stubreturn width;Overridepublic int getY1() / TODO Auto-generated method stubreturn topY;Overridepublic int getY2() / TODO Auto-generated method stubreturn height;MyRectangle.java:package 多態(tài)性1;import java.awt.Graphics;import java.awt

22、.Graphics2D;import java.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;public class MyRectangle extends Object implements MyShape private int leftX, topY, width,height;public MyRectangle(int x1,int y1,int x2,int y2) / TODO Auto-generated constructor stubsetX1(x1);setY1(y1);setX2(x2);setY2(y2);O

23、verridepublic void draw(Graphics g) / TODO Auto-generated method stubGraphics2D g2 = (Graphics2D) g;/ 畫矩形Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);g2.draw(rect);Overridepublic void setX1(int x1) / TODO Auto-generated method stubleftX=x1;Overridepublic void setX2(int x2) /

24、 TODO Auto-generated method stubwidth=x2;Overridepublic void setY1(int y1) / TODO Auto-generated method stubtopY=y1;Overridepublic void setY2(int y2) / TODO Auto-generated method stubheight=y2;Overridepublic int getX1() / TODO Auto-generated method stubreturn leftX;Overridepublic int getX2() / TODO

25、Auto-generated method stubreturn width;Overridepublic int getY1() / TODO Auto-generated method stubreturn topY;Overridepublic int getY2() / TODO Auto-generated method stubreturn height;TestDrawWindow代碼:package 多態(tài)性1;/TestDrawWindow.java/Program randomly draws shapesimport java.awt.*;import javax.swin

26、g.*;public class TestDrawWindow extends JFrame /* * */private static final long serialVersionUID = 1L;private MyShape shape = new MyShape15; /Constructor public TestDrawWindow() super(Draw shapes); int shapeType; /random value to determine shape to create and draw int x1,y1,x2,y2; /randomly chosen coordinates for a new shape for(int i=0;ishape.length;i+) x1=(int)(Math.random()*400); x2=(int)(Math.random()*400); y1=(int)(Math.random()*400); y2=(int)(Math.random()*400); shapeType=(int)(Math.random()*3)+1; switch(shapeType) case 1: /line shapei=new MyLine(x1,y1,x2,y2);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論