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