Java双语实验报告 Polymorphism and Package.docx

上传人:b****6 文档编号:6835738 上传时间:2023-01-11 格式:DOCX 页数:12 大小:33.91KB
下载 相关 举报
Java双语实验报告 Polymorphism and Package.docx_第1页
第1页 / 共12页
Java双语实验报告 Polymorphism and Package.docx_第2页
第2页 / 共12页
Java双语实验报告 Polymorphism and Package.docx_第3页
第3页 / 共12页
Java双语实验报告 Polymorphism and Package.docx_第4页
第4页 / 共12页
Java双语实验报告 Polymorphism and Package.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

Java双语实验报告 Polymorphism and Package.docx

《Java双语实验报告 Polymorphism and Package.docx》由会员分享,可在线阅读,更多相关《Java双语实验报告 Polymorphism and Package.docx(12页珍藏版)》请在冰豆网上搜索。

Java双语实验报告 Polymorphism and Package.docx

Java双语实验报告PolymorphismandPackage

Report

Class:

Name:

No.:

Course

JavaProgrammingLanguage

Experiment

Name

PolymorphismandPackage

Type

Verify

Demo

Integrated

Design

Teacher

Score

1.Goalofthisexperiment

(1)Knowhowtousejavainpolymorphic.

(2)Useobjects,classes,andpackagesupthetransition.

(3)Useconstructiontoolsandachievejavaapplication

2.Procedures

(1)Aprogram,Usingpackage,finalandabstractkeyword:

①Enterthefollowingprocedureandunderstandpackage,finalandabstractkeyword.

②Modifythesourcecodetomakeiteffective.Summaryofprogramfeaturesandlearnhowtousethepackage,finalandabstractkeyword.

③Notetheuseofthepackageprogrammustbecompiledwiththefollowingcommand

javac–d.PackTest.java

javacn.mybole.PackTest

④Saytheroleofeachline

⑤Tellhowtorunthisprogramonanothercomputer

(2)Acomplexprocess:

①Enterthefollowingprogramandunderstandabstractclassesandinheritance.

②Modifythesourcecodetomakeitbetter.Summaryofprogramfeaturesandlearnhowtousepolymorphism.

③Tellthemeaningofeachlineofcode.

(3)Astaticpolymorphismprogram

①Enterthefollowingstaticmethodsandproceduresandunderstandpolymorphism.

②Runtheprogramoutcome.

③Summaryofprogramfeaturesandlearnhowtousepolymorphism.

④Tellthemeaningofeachlineofcode

(4)Addanattributewagesalaryinexampleone.Modifythesourcecodetotrytousepolymorphismtoachieve.

3.Recordingoforiginaldataorresult

(1)oneprogram,usepackage,finalandabstractkeyword:

packagecom.bakahoshi.main;

publicabstractclassTest{

publicfinalvoidpubMethod(){

System.out.println("pubMethod");

}

protectedabstractvoidproMethod();

voiddefMethod(){

System.out.println("defMethod");

}

privatevoidpriMethod(){

System.out.println("priMethod");

}

publicstaticvoidmain(String[]args){

Testt=newTest(){

@Override

protectedvoidproMethod(){

//TODOAuto-generatedmethodstub

System.out.println("proMethod");

}

};

t.pubMethod();

t.proMethod();

t.defMethod();

t.priMethod();

}

}

Useananonymousclassthatimplementstheabstractclasstospecificimplementation-definedmethod

(2)Acomplexprocess:

packagecom.bakahoshi.main;

abstractclassEmployee{

privateStringname;

publicEmployee(Stringname){

this.setName(name);

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicabstractdoublecomputeSalary();

}

classManagerextendsEmployee{

privatedoublemonthSalary;

publicManager(Stringname,doublemonthSalary){

super(name);

this.monthSalary=monthSalary;

}

@Override

publicdoublecomputeSalary(){

returnmonthSalary;

}

/**

*@returnthemonthSalary

*/

publicdoublegetMonthSalary(){

returnmonthSalary;

}

/**

*@parammonthSalarythemonthSalarytoset

*/

publicvoidsetMonthSalary(doublemonthSalary){

this.monthSalary=monthSalary;

}

}

classSalesmanextendsEmployee{

privatedoublebaseSalary;

privatedoublecommision;

privateintqualtities;

publicSalesman(Stringname,doublebaseSalary,

doublecommision,intqualtities){

super(name);

this.baseSalary=baseSalary;

mision=commision;

this.qualtities=qualtities;

}

@Override

publicdoublecomputeSalary(){

returnbaseSalary+commision*qualtities;

}

/**

*@returnthebaseSalary

*/

publicdoublegetBaseSalary(){

returnbaseSalary;

}

/**

*@parambaseSalarythebaseSalarytoset

*/

publicvoidsetBaseSalary(doublebaseSalary){

this.baseSalary=baseSalary;

}

/**

*@returnthecommision

*/

publicdoublegetCommision(){

returncommision;

}

/**

*@paramcommisionthecommisiontoset

*/

publicvoidsetCommision(doublecommision){

mision=commision;

}

/**

*@returnthequaltities

*/

publicintgetQualtities(){

returnqualtities;

}

/**

*@paramqualtitiesthequaltitiestoset

*/

publicvoidsetQualtities(intqualtities){

this.qualtities=qualtities;

}

}

classWorkerextendsEmployee{

privatedoubledailySalary;

privateintdays;

publicWorker(Stringname,doubledailySalary,intdays){

super(name);

this.dailySalary=dailySalary;

this.days=days;

}

@Override

publicdoublecomputeSalary(){

returndailySalary*days;

}

/**

*@returnthedailySalary

*/

publicdoublegetDailySalary(){

returndailySalary;

}

/**

*@paramdailySalarythedailySalarytoset

*/

publicvoidsetDailySalary(doubledailySalary){

this.dailySalary=dailySalary;

}

/**

*@returnthedays

*/

publicintgetDays(){

returndays;

}

/**

*@paramdaysthedaystoset

*/

publicvoidsetDays(intdays){

this.days=days;

}

}

publicclassEmployeeDemo{

publicstaticvoidmain(Stringargs[]){

Managere1=newManager("John",10000);

Salesmane2=newSalesman("Smith",2000,50.4,63);

Workere3=newWorker("Bill",79.5,28);

System.out.println("Manager"+e1.getName()+"salary:

"

+puteSalary());

System.out.println("Salesman"+e2.getName()+"salary:

"

+puteSalary());

System.out.println("Worker"+e3.getName()+"salary:

"

+puteSalary());

}

}

 

(3)Aprogramusingstaticpolymorphism

packagecom.bakahoshi.main;

classStaticSuper{

publicstaticStringstaticGet(){

return"BasestaticGet()";

}

publicStringdynamicGet(){

return"BasedynamicGet()";

}

}

classStaticSubextendsStaticSuper{

publicstaticStringstaticGet(){

return"DerivedstaticGet()";

}

publicStringdynamicGet(){

return"DeriveddynamicGet()";

}

}

publicclassStaticPolymorphism{

publicstaticvoidmain(String[]args){

StaticSupersup=newStaticSub();//Upcast

System.out.println(StaticSuper.staticGet());

System.out.println(sup.dynamicGet());

}

}

Theresult:

BasestaticGet()

DeriveddynamicGet()

Reason:

Staticmethodsaremembersbelongtotheclassmembers,ratherthaninstancesofobjects

 

(4)Procedure:

AftermodifyingtheEmployeeclasscode

abstractclassEmployee{

privateStringname;

privatedoublesalary;

publicEmployee(Stringname){

this.setName(name);

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicabstractdoublecomputeSalary();

/**

*@returnthesalary

*/

publicdoublegetSalary(){

returnsalary;

}

/**

*@paramsalarythesalarytoset

*/

publicvoidsetSalary(doublesalary){

this.salary=salary;

}

}

 

4.Resultandanalysis

(1)Programwithonepackage、finalandabstractkeyword:

Analysis:

javainallinstancemethods(ienon-staticmethodmodified)andnon-finalmethodsarevirtualmethodsaredynamicallyassignedatruntime,soallmethodcallswillbebasedontheactualtypeoftheobjectratherthanthestatictypeofdispatchmethod.Thestaticmethods,finalmethods,theparentclassmethodsandinstanceconstructorscollectively,includingnon-virtualmethoddispatchthesemethodsatcompileanalyticalmethodshavebeencompleted.

(2)Acomplexprocess:

Analysis:

dittoaproblem:

allinstancesofmethodsarevirtualmethods

(3)Amulti-stateandthestatickeywordprogram

Analysis:

Staticmethodsaremembersbelongtotheclassmembers,ratherthaninstancesofobjects

Signature:

Date:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 文学研究

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1