Java实验报告:深入解析继承与多态在Shape类中的应用
Java程序设计实验报告继承与多态
packageMyShape;
publicclassTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Circlec=newCircle(2,4,3);
c.printItMyWay();
}
}
abstractclassShape{
publicabstractfloatgetCir();
publicabstractfloatgetArea();
}
classPointextendsShapeiimplementsPrintable{
publicintx;
publicinty;
publicPoint(intx,inty){
this.x=x;
this.y=y;
}
publicPoint(){
}
@Override
publicfloatgetCir(){
//自动生成方法的TODOStub
return0;
}
@Override
publicfloatgetArea(){
//TODO自动生成的方法存根
return0;
}
页面ublicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
返回;
}
publicvoidsetY(inty){
this.y=y;
}
@Override
publicvoidprintItMyWay(){
System.out.println("Point("+x+","+y+")");
System.out.println("PointArea:"+this.getArea());
System.out.println("圆点:"+this.getCir());
}
}
classCircleextendsPointimplementsPrintable{
publicfloatr;
publicCircle(){
}
publicCircle(floatr,intx,inty){
0){this.r=r;
this.x=x;
this.y=y;
}
}
publicfloatgetR(){
returnr;
}
publicvoidsetR(floatr){
this.r=r;
}
@Override
publicfloatgetArea(){
return(float)(r*r*3.14/2);
}
@Override
publicfloatgetCir(){
return(float)(3.14*r*2);
}
@Override
publicvoidprintItMyWay(){
System.out.println("圆("+x+","+y+")");
System.out.println("CircleR:"+r);
System.out.println("CircleArea:"+this.getArea());
System.out.println("CircleCircle:"+this.getCir());
}
}
interfacePrintable{
publicvoidprintItMyWay();
}