導航:首頁 > 編程語言 > java介面習題

java介面習題

發布時間:2022-09-28 23:07:43

java介面的一道習題

//抽象類Person

packageA;

publicabstractclassPerson{

Stringname=null;

Stringaddress=null;

publicabstractvoidAddWage();

}

//實現&測試類

packageA;

classManagerextendsEmployee{

Stringlevel;

publicManager(Stringname,Stringaddress,StringID,Doublewage,intworkYear,Stringlevel){

super(name,address,ID,wage,workYear);

this.level=level;

}

publicvoidAddWage(){

wage=wage*1.2;

}

publicStringtoString(){

return"Name:"+name+" "+

"Address:"+address+" "+

"ID:"+ID+" "+

"Wage:"+wage+" "+

"WorkYear:"+workYear+" "+

"ManagerLevel:"+level+" ";

}

}

classEmployeeextendsPerson{

StringID;

Doublewage;

intworkYear;

publicEmployee(Stringname,Stringaddress,StringID,Doublewage,intworkYear){

this.name=name;

this.address=address;

this.ID=ID;

this.wage=wage;

this.workYear=workYear;

}

publicvoidAddWage(){

wage=wage*1.1;

}

publicStringtoString(){

return"Name:"+name+" "+

"Address:"+address+" "+

"ID:"+ID+" "+

"Wage:"+wage+" "+

"WorkYear:"+workYear+" ";

}

}

publicclassTestManager{

publicstaticvoidmain(String[]args){

Employeee=newEmployee("Tony","USA","209881",20000D,4);

Managerm=newManager("Bush","China","209999",100000D,10,"D");

System.out.println(e+"----------- "+m);

}

}

㈡ Java關於介面的練習題。求幫忙解答.

importjava.util.LinkedList;
importjava.util.List;

publicclassTestInterface{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
List<Shape>list=newLinkedList<Shape>();
list.add(newCircle(1));
list.add(newSphere(1));
list.add(newCylinder(1,1));
for(Shapes:list)
System.out.println(s.getClass().getName()+" "+s.area()+" "+s.volume());

}

}
interfaceShape{
publicdoublearea();
publicdoublevolume();
}

classCircleimplementsShape{
privatedoubler;

publicCircle(doubler){
this.r=r;
}

publicdoublearea(){
returnMath.PI*r*r;
}

publicdoublevolume(){
return0;
}

publicdoublegetR(){
returnr;
}

publicvoidsetR(doubler){
this.r=r;
}
}

classSphereimplementsShape{
privatedoubler;

publicSphere(doubler){
this.r=r;
}

publicdoublearea(){
return4*Math.PI*r*r;
}

publicdoublevolume(){
return(4.0/3.0)*Math.PI*r*r*r;
}

publicdoublegetR(){
returnr;
}

publicvoidsetR(doubler){
this.r=r;
}
}

classCylinderimplementsShape{
privatedoubler;
privatedoubleheight;
publicCylinder(doubler,doubleheight){
this.r=r;
this.height=height;
}

publicdoublearea(){
return2*Math.PI*r*(r+height);
}

publicdoublevolume(){
returnMath.PI*r*r*height;
}

publicdoublegetR(){
returnr;
}

publicvoidsetR(doubler){
this.r=r;
}

publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
}

㈢ JAVA習題 抽象類和介面問題

可以這么理解. 需要注意的是,一個子類只能繼承一個父類,但是卻可以實現多個介面,所以盡量使用介面.也有這么一個說法,繼承會破壞類的封裝性.

㈣ JAVA 介面練習題


publicinterfaceIshape{
/**
*求周長
*@return
*/
intgetPerimeter();

/**
*求面積
*@return
*/
intgetArea();
}
{

privateintlength;

privateintwidth;

publicTriangle(intlength,intwidth){
this.length=length;
this.width=width;
}

@Override
publicintgetPerimeter(){
return(length+width)*2;
}

@Override
publicintgetArea(){
returnlength*width;
}
}
{

privateintline1;
privateintline2;
privateintline3;
privateintheight1;
privateintheight2;
privateintheight3;

publicRectangle(intline1,intline2,intline3,intheight1,intheight2,intheight3){
this.line1=line1;
this.line2=line2;
this.line3=line3;
this.height1=height1;
this.height2=height2;
this.height3=height3;
}

@Override
publicintgetPerimeter(){
returnline1+line2+line3;
}

@Override
publicintgetArea(){
returnline1*height1/2;
}
}
/**
*Createdbytonyon2015/11/6.
*平行四邊形
*/
{

privateintline1;
privateintline2;
privateintheight1;
privateintheight2;

publicParallelogram(intline1,intline2,intheight1,intheight2){
this.line1=line1;
this.line2=line2;
this.height1=height1;
this.height2=height2;
}

@Override
publicintgetPerimeter(){
return(line1+line2)*2;
}

@Override
publicintgetArea(){
returnline1*height1;
}
}
/**
*Createdbytonyon2015/11/6.
*梯形
*/
{

privateinttopLine;
privateintbottomLine;
privateintline1;
privateintline2;
privateintheight;

publicEchelon(inttopLine,intbottomLine,intline1,intline2,intheight){
this.topLine=topLine;
this.bottomLine=bottomLine;
this.line1=line1;
this.line2=line2;
this.height=height;
}

@Override
publicintgetPerimeter(){
returntopLine+bottomLine+line1+line2;
}

@Override
publicintgetArea(){
return(topLine+bottomLine)*height/2;
}
}

publicclassTestShape{
publicstaticvoidmain(String[]args){
//矩形
Triangletriangle=newTriangle(4,5);

//三角形
Rectanglerectangle=newRectangle(3,4,5,4,3,6);

//平行四邊形
Parallelogramparallelogram=newParallelogram(6,7,4,5);

//梯形
Echelonechelon=newEchelon(3,4,6,7,8);

System.out.println("矩形周長:"+triangle.getPerimeter());
System.out.println("矩形面積:"+triangle.getArea());

System.out.println("三角形周長:"+rectangle.getPerimeter());
System.out.println("三角形面積:"+rectangle.getArea());

System.out.println("平行四邊形周長:"+parallelogram.getPerimeter());
System.out.println("平行四邊形面積:"+parallelogram.getArea());

System.out.println("梯形周長:"+echelon.getPerimeter());
System.out.println("梯形面積:"+echelon.getArea());
}
}

閱讀全文

與java介面習題相關的資料

熱點內容
壓縮因子定義 瀏覽:968
cd命令進不了c盤怎麼辦 瀏覽:214
葯業公司招程序員嗎 瀏覽:974
毛選pdf 瀏覽:659
linuxexecl函數 瀏覽:727
程序員異地戀結果 瀏覽:374
剖切的命令 瀏覽:229
干什麼可以賺錢開我的世界伺服器 瀏覽:290
php備案號 瀏覽:990
php視頻水印 瀏覽:167
怎麼追程序員的女生 瀏覽:487
空調外壓縮機電容 瀏覽:79
怎麼將安卓變成win 瀏覽:459
手機文件管理在哪兒新建文件夾 瀏覽:724
加密ts視頻怎麼合並 瀏覽:775
php如何寫app介面 瀏覽:804
宇宙的琴弦pdf 瀏覽:396
js項目提成計算器程序員 瀏覽:944
pdf光子 瀏覽:834
自拍軟體文件夾名稱大全 瀏覽:328