11.声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有GetArea( )函数计算对象的面积,
1个回答
展开全部
------------ Shape.java -------------
public abstract class Shape{
public abstract int getArea();
}
----------- Rectangle.java --------
public class Rectangle extends Shape{
int width;
int height;
public Rectangle(int width, height){ this.width=width; this.height=height;}
public int getArea() { return width*height;}
}
----------- Circle.java -------------
public class Circle extends Shape{
int r; //半径
public Circle(int r){ this.r = r; }
public int getArea() { return Math.PI * r * r; }
}
------------- Test.java -------------
public class Test{
public static void main(String[] args){
Shape rect = new Rectangle(20, 10);
System.out.println("Rectangle(20,10) area is: " + rect.getArea());
Shape circle = new Circle(5);
System.out.println("Circle(5) area is: " + circle.getArea());
}
}
满意请采纳!
public abstract class Shape{
public abstract int getArea();
}
----------- Rectangle.java --------
public class Rectangle extends Shape{
int width;
int height;
public Rectangle(int width, height){ this.width=width; this.height=height;}
public int getArea() { return width*height;}
}
----------- Circle.java -------------
public class Circle extends Shape{
int r; //半径
public Circle(int r){ this.r = r; }
public int getArea() { return Math.PI * r * r; }
}
------------- Test.java -------------
public class Test{
public static void main(String[] args){
Shape rect = new Rectangle(20, 10);
System.out.println("Rectangle(20,10) area is: " + rect.getArea());
Shape circle = new Circle(5);
System.out.println("Circle(5) area is: " + circle.getArea());
}
}
满意请采纳!
更多追问追答
追问
我没学,有C++吗?
追答
下次提问题时一定要注意。别人怎么知道你想要的是java的、C++的还是C#的呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询