导航:首页 > 编程语言 > 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接口习题相关的资料

热点内容
php怎么跳转到电脑 浏览:413
如何在电脑上创建新网络连接服务器 浏览:61
c语言编译之后如何运行 浏览:566
mfc多线程编程视频 浏览:410
c编译的中文怎么写 浏览:91
单片机连接蜂鸣器电路 浏览:844
程序员买房前后对比照 浏览:988
cmdjava中文乱码 浏览:947
窗口app哪个好 浏览:731
xzforandroid 浏览:577
程序员那么可爱歌曲完整版 浏览:906
为什么购买pdf 浏览:45
操作系统代码编译 浏览:483
程序员东北大学 浏览:426
编译忽略空字符 浏览:118
多店铺阿里云服务器教程 浏览:379
单片机求初值 浏览:421
安卓机如何在电脑备份图片 浏览:927
ca证书加密机价格 浏览:799
天干地支年份算法 浏览:798