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