實(shí)驗(yàn)五_繼承與接口.doc_第1頁(yè)
實(shí)驗(yàn)五_繼承與接口.doc_第2頁(yè)
實(shí)驗(yàn)五_繼承與接口.doc_第3頁(yè)
實(shí)驗(yàn)五_繼承與接口.doc_第4頁(yè)
實(shí)驗(yàn)五_繼承與接口.doc_第5頁(yè)
已閱讀5頁(yè),還剩22頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

實(shí)驗(yàn)五 繼承與接口1. 實(shí)驗(yàn)?zāi)康?、掌握類的繼承細(xì)節(jié)內(nèi)容,包括子類的繼承、子類對(duì)象創(chuàng)建、成員變量的繼承與隱藏、方法的繼承與重寫(xiě)2、掌握重寫(xiě)的目的以及怎樣使用super關(guān)鍵字3、掌握上轉(zhuǎn)型對(duì)象與接口回調(diào)技術(shù)4、掌握類怎樣實(shí)現(xiàn)接口,及面向接口的編程思想2. 實(shí)驗(yàn)內(nèi)容1、根據(jù)附錄里的源代碼,按照注釋要求,完成代碼填空,使程序能夠運(yùn)行得出結(jié)果。1)實(shí)驗(yàn)1中國(guó)人與美國(guó)人2)實(shí)驗(yàn)2銀行與利息3)實(shí)驗(yàn)3面積之和4)實(shí)驗(yàn)4歌手大賽5)實(shí)驗(yàn)5天氣預(yù)報(bào)2、設(shè)計(jì)編寫(xiě)程序完成以下任務(wù)。1)根據(jù)要求修改實(shí)驗(yàn)2,參照建設(shè)銀行再編寫(xiě)一個(gè)商業(yè)銀行(廣發(fā)行),讓程序輸出8000元在廣發(fā)行8年零212天的利息。2) 根據(jù)要求修改實(shí)驗(yàn)3,再增加一種幾何圖形(梯形),并讓主類中的tuxing的某些元素是梯形的上轉(zhuǎn)型對(duì)象。3)仿照實(shí)驗(yàn)5編寫(xiě)一個(gè)程序?qū)崿F(xiàn)模擬水杯中的水在不同溫度下可能出現(xiàn)的狀態(tài)。4)編寫(xiě)一個(gè)接口并創(chuàng)建兩個(gè)實(shí)現(xiàn)該接口的類A、B。A、B類實(shí)現(xiàn)了接口的f方法,A類的f方法內(nèi)容為計(jì)算1!+3!+5!+9!并返回結(jié)果,B類的f方法內(nèi)容為計(jì)算2!+4!+6!+10!也返回結(jié)果;再編一個(gè)執(zhí)行類,執(zhí)行類運(yùn)行時(shí)要求通過(guò)接口回調(diào)方式用同一個(gè)對(duì)象實(shí)例分別調(diào)用A類的f方法和B類的f方法。5)(1)定義一個(gè)汽車類Vehicle,要求如下:(知識(shí)點(diǎn):類的繼承 方法的覆蓋)(a)屬性包括:汽車品牌brand(String類型)、顏色color(String類型)和速度speed(double類型)。(b)至少提供一個(gè)有參的構(gòu)造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)。(c)為屬性提供訪問(wèn)器方法。注意:汽車品牌一旦初始化之后不能修改。(d)定義一個(gè)一般方法run(),用打印語(yǔ)句描述汽車奔跑的功能。定義測(cè)試類VehicleTest,在其main方法中創(chuàng)建一個(gè)品牌為“benz”、顏色為“black”的汽車。(2)定義一個(gè)Vehicle類的子類轎車類Car,要求如下:(a)轎車有自己的屬性載人數(shù)loader(int 類型)。(b)提供該類初始化屬性的構(gòu)造方法。(c)重新定義run(),用打印語(yǔ)句描述轎車奔跑的功能。(d)定義測(cè)試類Test,在其main方法中創(chuàng)建一個(gè)品牌為“Honda”、顏色為“red”,載人數(shù)為2人的轎車。面向?qū)ο蠡A(chǔ)出過(guò)類似的,這個(gè)把繼承引入了6)Cola公司的雇員分為以下若干類:(知識(shí)點(diǎn):多態(tài))(1) ColaEmployee :這是所有員工總的父類,屬性:?jiǎn)T工的姓名,員工的生日月份。方法:getSalary(int month) 根據(jù)參數(shù)月份來(lái)確定工資,如果該月員工過(guò)生日,則公司會(huì)額外獎(jiǎng)勵(lì)100 元。(2) SalariedEmployee :ColaEmployee 的子類,拿固定工資的員工。屬性:月薪(3) HourlyEmployee :ColaEmployee 的子類,按小時(shí)拿工資的員工,每月工作超出160 小時(shí)的部分按照1.5 倍工資發(fā)放。屬性:每小時(shí)的工資、每月工作的小時(shí)數(shù)(4) SalesEmployee :ColaEmployee 的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率(5) 定義一個(gè)類Company,在該類中寫(xiě)一個(gè)方法,調(diào)用該方法可以打印出某月某個(gè)員工的工資數(shù)額,寫(xiě)一個(gè)測(cè)試類TestCompany,在main方法,把若干各種類型的員工放在一個(gè)ColaEmployee 數(shù)組里,并單元出數(shù)組中每個(gè)員工當(dāng)月的工資。知識(shí)點(diǎn):繼承與多態(tài)7)利用接口實(shí)現(xiàn)動(dòng)態(tài)的創(chuàng)建對(duì)象:(知識(shí)點(diǎn):接口 )(1)創(chuàng)建4個(gè)類1蘋(píng)果2香蕉3葡萄4園丁(2)在三種水果的構(gòu)造方法中打印一句話.以蘋(píng)果類為例class applepublic apple()System.out.println(“創(chuàng)建了一個(gè)蘋(píng)果類的對(duì)象”);(3)類圖如下:(4)要求從控制臺(tái)輸入一個(gè)字符串,根據(jù)字符串的值來(lái)判斷創(chuàng)建三種水果中哪個(gè)類的對(duì)象。運(yùn)行結(jié)果如圖:3. 實(shí)驗(yàn)步驟實(shí)驗(yàn)一、1實(shí)驗(yàn)一、2實(shí)驗(yàn)一、3實(shí)驗(yàn)一、4實(shí)驗(yàn)一、5實(shí)驗(yàn)二、1package SFirst;public class Bank int savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate(double rate) interestRate = rate;package SFirst;public class GuangFaBank extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=puterInterest();/【代碼1】/super調(diào)用隱藏的computerInterest()方法double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;return interest;package SFirst;public class SaveMoney public static void main(String args) int amount=8000;GuangFaBank bank1=new GuangFaBank();bank1.savedMoney=amount;bank1.year=8.212;bank1.setInterestRate(0.035);double interest1=puterInterest();System.out.println(8000元在廣發(fā)行8年零212天的利息為:+interest1);實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)二、2package SSecond;public abstract class Geometry public abstract double getArea();package SSecond;public class MainClass public static void main(String args) Trapezoid tuxing=new Trapezoid(4.7,7.9,7);/有29個(gè)Geometry對(duì)象double s=tuxing.getArea();System.out.printf(梯形的面積為:n%f,s);package SSecond;public class Trapezoid extends Geometrydouble a,b,h;Trapezoid(double a,double b,double h)this.a=a;this.b=b;this.h=h;/【代碼2】/重寫(xiě)getArea()方法Overridepublic double getArea() / TODO Auto-generated method stubreturn (a+b)*h/2;實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)二、3package SThird;public class BoiledState implements WaterStateOverridepublic void showState() / TODO Auto-generated method stubSystem.out.println(開(kāi)水。);/【代碼3】/重寫(xiě)public void showState()方法package SThird;public class IceState implements WaterStatepublic void showState()System.out.print(結(jié)冰。);package SThird;public class LittleWarmState implements WaterState/【代碼1】/重寫(xiě)public void showState()方法Overridepublic void showState() / TODO Auto-generated method stubSystem.out.println(微涼。);package SThird;public class WarmState implements WaterStateOverridepublic void showState() / TODO Auto-generated method stubSystem.out.println(溫水。);/【代碼2】/重寫(xiě)public void showState()方法package SThird;public class Water WaterState state;public void show()state.showState();public void setState(WaterState s)state=s;package SThird;public class WaterForecast public static void main(String args) Water waterState=new Water();System.out.print(n溫度在90-100時(shí)水杯水的狀態(tài):);waterState.setState(new BoiledState();waterState.show();System.out.print(n溫度在50-80時(shí)水杯水的狀態(tài):);waterState.setState(new WarmState();waterState.show();System.out.print(n溫度在30-50時(shí)水杯水的狀態(tài):);waterState.setState(new LittleWarmState();waterState.show();System.out.print(n溫度在0度下時(shí)水杯水的狀態(tài):);waterState.setState(new IceState();waterState.show();package SThird;public interface WaterState public void showState();實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)四package SForth;public class A implements object public int f(int a, int b) int sum=0,temp=1;for(int i=a;i=b;i=i+2)for(int j=1;j=i;j+)temp*=j;sum+=temp;temp=1;return sum;package SForth;public class B implements object public int f(int a, int b) int sum=0,temp=1;for(int i=a;i=b;i=i+2)for(int j=1;j=i;j+)temp*=j;sum+=temp;temp=1;return sum;package SForth;public interface object int f(int a,int b);package SForth;public class Test public static void main(String args) / TODO Auto-generated method stubA a=new A();B b=new B();int sum1=a.f(1, 9);int sum2=b.f(2, 10);System.out.println(1!+3!+5!+9!的結(jié)果為:+sum1);System.out.println(2!+4!+6!+10!的結(jié)果為:+sum2);實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)五package SFifth;public class Car extends Vehicle int loader;public Car(String brand, String color, double speed,int l) super(brand, color, speed);this.loader=l;/ TODO Auto-generated constructor stubpublic void run()System.out.println(顏色為+super.getColor()+的+super.getBrand()+汽車的時(shí)速為:+super.getSpeed();package SFifth;public class Test public static void main(String args) / TODO Auto-generated method stubCar c=new Car(Honda,red,300.0,20);c.run();package SFifth;public class Vehicle private String brand=BMW;/汽車品牌private String color=red;/汽車顏色private double speed=0;/汽車時(shí)速public String getColor() return color;public void setColor(String color) this.color = color;public double getSpeed() return speed;public void setSpeed(double speed) this.speed = speed;public String getBrand() return brand;public Vehicle(String brand, String color, double speed) / TODO Auto-generated constructor stubthis.brand=brand;this.color=color;this.speed=speed;public void run() System.out.println(brand+汽車的時(shí)速為:+speed);package SFifth;public class VehicleTest public static void main(String args) / TODO Auto-generated method stubVehicle v=new Vehicle(benz,black,200.0);v.run();實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)六package SSixth;public class ColaEmployee String name;/員工姓名int month;/員工生日月份public String getName() return name;public void setName(String name) = name;public int getMonth() return month;public void setMonth(int month) this.month = month;int getSalary(int month) if (month = this.month) return 100; else return 0; package SSixth;public class Company extends ColaEmployeevoid print(String name, double salary,int month)String Name=name;double Salary=salary;int Month=month;System.out.println(員工+Name+在+Month+月+的薪資為:+Salary);package SSixth;public class HourlyEmployee extends ColaEmployee double hours;/每月工作的時(shí)間double hsalary;/每小時(shí)的工資Company c=new Company();public HourlyEmployee(String name,int month, double hours, double hsalary) / TODO Auto-generated constructor stubthis.hours=hours;this.hsalary=hsalary;this.month=month;=name;public double getHours() return hours;public void setHours(double hours) this.hours = hours;public double getHsalary() return hsalary;public void setHsalary(double hsalary) this.hsalary = hsalary;public int getSalary(int month)if(this.hours=160)this.hsalary=this.hsalary*this.hours+super.getSalary(month);c.print(name,hsalary,month);return 0;else this.hsalary=(this.hours-160)*this.hsalary*1.5+160*this.hsalary+super.getSalary(month);c.print(name,hsalary,month);return 0;package SSixth;public class SalariedEmployee extends ColaEmployee double salary;/員工固定薪資(按月)Company c=new Company();public SalariedEmployee(String name, int month, double salary) / TODO Auto-generated constructor =name;this.month=month;this.salary=salary;public int getSalary(int month) this.salary=salary + super.getSalary(month); c.print(name,salary,month);return 0; package SSixth;public class SalesEmployee extends ColaEmployeedouble money;/月銷售額double ticheng;/提成率Company c=new Company();public SalesEmployee(String name, int month, double money, double ticheng) / TODO Auto-generated constructor stubthis.money=money;this.month=month;=name;this.ticheng=ticheng;public double getMoney() return money;public void setMoney(double money) this.money = money;public double getTicheng() return ticheng;public void setTicheng(double ticheng) this.ticheng = ticheng;public int getSalary(int month) this.money= money + super.getSalary(month)+(money*ticheng); c.print(name,money,month); return 0; package SSixth;public class SalesEmployee extends ColaEmployeedouble money;/月銷售額double ticheng;/提成率Company c=new Company();public SalesEmployee(String name, int month, double money, double ticheng) / TODO Auto-generated constructor stubthis.money=money;this.month=month;=name;this.ticheng=ticheng;public double getMoney() return money;public void setMoney(double money) this.money = money;public double getTicheng() return ticheng;public void setTicheng(double ticheng) this.ticheng = ticheng;public int getSalary(int month) this.money= money + super.getSalary(month)+(money*ticheng); c.print(name,money,month); return 0; package SSixth;public class TestCompany public static void main(String args) / TODO Auto-generated method stubColaEmployee ce =new ColaEmployee3;ce0=new SalariedEmployee(aa,9,3000.0);ce1=new HourlyEmployee(bb,3,2000.0,180);ce2=new SalesEmployee(cc,6,6000,0.3);ce0.getSalary(2);ce1.getSalary(2);ce2.getSalary(2);實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)七package SSeventh;public class apple implements Fruit public void apple() / TODO Auto-generated method stubSystem.out.println(創(chuàng)建了一個(gè)蘋(píng)果類的對(duì)象);Overridepublic void banana() / TODO Auto-generated method stubOverridepublic void pear() / TODO Auto-generated method stubpackage SSeventh;public class banana implements Fruit public void banana() / TODO Auto-generated method stubSystem.out.println(創(chuàng)建了一個(gè)香蕉類的對(duì)象);Overridepublic void apple() / TODO Auto-generated method stubOverridepublic void pear() / TODO Auto-generated method stubpackage SSeventh;public interface Fruit void apple(); void banana(); void pear();package SSeventh;public class pear implements Fruit public void pear() / TODO Auto-generated method stubSystem.out.println(創(chuàng)建了一個(gè)梨子類的對(duì)象);Overridepublic void apple() / TODO Auto-generated method stubOverridepublic void banana() / TODO Auto-generated method stubpackage SSeventh;import java.util.Scanner;public class Test public static void main(String args) / TODO Auto-generated method stub/Fruit f=new banana();Scanner input=new Scanner(System.in);System.out.println(請(qǐng)輸入您要?jiǎng)?chuàng)建的類:);String attr=input.nextLine();if(attr.equals(banana)Fruit fruit=new banana();fruit.banana();else if(attr.equals(apple)Fruit fruit = new apple();fruit.apple();else if(attr.equals(pear)Fruit fruit =new pear();fruit.pear();else System.out.println(輸入有誤!);實(shí)驗(yàn)結(jié)果4. 評(píng)分標(biāo)準(zhǔn)1. A內(nèi)容功能完善,編程風(fēng)格好,人機(jī)接口界面好; 2. B內(nèi)容功能完善,編程風(fēng)格良好,人機(jī)接口界面良好;3. C完成必做內(nèi)容;4. D能完成必做內(nèi)容;5. E未按時(shí)完成必做內(nèi)容,或者抄襲(雷同者全部為E).參照書(shū)上實(shí)驗(yàn)按模版要求,將【代碼】替換為Java程序代碼,編寫(xiě)好完整的程序文檔,最后運(yùn)行得到的相關(guān)文件,把實(shí)驗(yàn)所得文件一起打包上交。(壓縮包的文件名為:學(xué)號(hào)后三位和名字開(kāi)頭字母,如109zhhRAR|ZIP)附錄:實(shí)驗(yàn)1 中國(guó)人與美國(guó)人模板代碼 People.javapublic class People protected double weight ,height;public void speakHello()System.out.println(yayayaya);public void averageHeight()height=173;System.out.println(average height:+height);public void averageWeight()weight=70;System.out.println(average weight:+weight);ChinaPeople.javapublic class ChinaPeople extends Peoplepublic void speakHello()System.out.println(你好);public void averageHeight()height=168.78;System.out.println(中國(guó)人的平均身高:+height+厘米);/【代碼1】/重寫(xiě)public void averageWeight()方法,輸出:中國(guó)人的平均體重:65公斤public void chinaGongfu()System.out.println(坐如鐘,站如松,睡如弓);AmericanPeople.javapublic class AmericanPeople extends People/【代碼2】/重寫(xiě)public void speakHello()方法,輸出:How do you do/【代碼3】/重寫(xiě)public void averageHeight()方法,輸出:Americans average height:176cmpublic void averageWeight()weight=75;System.out.println(Americans average weight:+weight+kg);public void americanBoxing()System.out.println(直拳、鉤拳、組合拳);BeijingPeople.javapublic class BeijingPeople extends ChinaPeople/【代碼4】/重寫(xiě)public void averageHeight()方法,輸出:北京人的平均身高:172.5厘米/【代碼5】/重寫(xiě)public void averageWeight()方法,輸出:北京人的平均體重:70公斤public void beijingOpera()System.out.println(花臉、青衣、花旦和老生);Example.javapublic class Example public static void main(String args) ChinaPeople chinaPeople = new ChinaPeople();AmericanPeople americanPeople = new AmericanPeople();BeijingPeople beijingPeople = new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();實(shí)驗(yàn)2 銀行與利息模板代碼 Bank.javapublic class Bank int savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate(double rate) interestRate = rate;ConstructionBank.javapublic class ConstructionBank extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=/【代碼1】/super調(diào)用隱藏的computerInterest()方法double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在建設(shè)銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;BankOfDalian.javapublic class BankOfDalian extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=/【代碼2】/super調(diào)用隱藏的computerInterest()方法double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在大連銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;SaveMoney.javapublic class SaveMoney public static void main(String args) int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=puterInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=puterInterest();System.out.printf(兩個(gè)銀行利息相差%f元n,interest2-interest1);實(shí)驗(yàn)3 面積之和模板代碼 Geometry.javapublic abstract class Geometry public abstract double getArea();TotalArea.javapublic class TotalArea Geometry tuxing;double totalArea=0;public void setTuxing(Geometry t)tuxing=t;public double computerTatalArea()/【代碼3】/用循環(huán)語(yǔ)句讓tuxing的元素調(diào)用getArea方法,并將返回的值累加到totalAreareturn totalArea;Rect.javapublic class Rect extends Geometrydouble a,b;Re

溫馨提示

  • 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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論