㈠ 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());
}
}