Java程序设计实验报告 继承与多态

发布网友 发布时间:2022-04-24 19:40

我来回答

1个回答

热心网友 时间:2022-05-03 05:11

package MyShape;


public class Test {


/**

* @param args

*/

public static void main(String[] args) {

Circle c = new Circle(2,4,3);

c.printItMyWay();

}


}

abstract class Shape{

public abstract float getCir();

public abstract float getArea();

}

class Point extends Shape implements Printable{

public int x;

public int y;

public Point(int x, int y){

this.x= x;

this.y=y;

}

public Point (){

}


@Override

public float getCir() {

// TODO Auto-generated method stub

return 0;

}

@Override

public float getArea() {

// TODO Auto-generated method stub

return 0;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

@Override

public void printItMyWay() {

System.out.println(" Point ("+x+","+y+")");

System.out.println(" Point Area:"+this.getArea());

System.out.println(" Point Circle:"+this.getCir());

}

}


class Circle extends Point implements Printable{

public float r ;

public Circle(){

}

public Circle(float r,int x, int y ){

        if(r>0){

        this.r =r;

        this.x =x;

        this.y=y;

}

}

public float getR() {

return r;

}

public void setR(float r) {

this.r = r;

}

@Override

public float getArea() {

return (float) (r*r*3.14/2);

}

@Override

public float getCir() {

return (float) (3.14*r*2);

}

@Override

public void printItMyWay() {

System.out.println(" Circle ("+x+","+y+")");

System.out.println(" Circle R:"+r);

System.out.println(" Circle Area:"+this.getArea());

System.out.println(" Circle Circle:"+this.getCir());

}

}


interface Printable {

public void printItMyWay();

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com