导航:首页 > 编程语言 > 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