用JAVA编写 声明一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积.
展开全部
public class Test{
public static void main(String args[]){
Shape rectangle = new Rectangle(3, 5);//长3宽5长方形
double rectangleArea = rectangle.getArea();
System.out.println("长3宽5的长方形面积是:" + rectangleArea);
Circle c = new Circle(3);//半径为3的圆
double circleArea = c.getArea();
System.out.println("半径为3的圆的面积为:" + circleArea);
}
}
abstract class Shape{
public abstract double getArea();
}
class Rectangle extends Shape{
private double height; //长
private double width; //宽
public Rectangle(double h, double w){
this.height = h;
this.width = w;
}
public double getArea() {
return height * width;
}
}
class Circle extends Shape{
private static final double PI = Math.PI;//圆周率,可以替换为3.14的按照需求啦
private double r ;//半径
public Circle(double r){
this.r = r;
}
public double getArea() {
return PI * r *r;
}
}
-----------------测试结果
长3宽5的长方形面积是:15.0
半径为3的圆的面积为:28.274333882308138
public static void main(String args[]){
Shape rectangle = new Rectangle(3, 5);//长3宽5长方形
double rectangleArea = rectangle.getArea();
System.out.println("长3宽5的长方形面积是:" + rectangleArea);
Circle c = new Circle(3);//半径为3的圆
double circleArea = c.getArea();
System.out.println("半径为3的圆的面积为:" + circleArea);
}
}
abstract class Shape{
public abstract double getArea();
}
class Rectangle extends Shape{
private double height; //长
private double width; //宽
public Rectangle(double h, double w){
this.height = h;
this.width = w;
}
public double getArea() {
return height * width;
}
}
class Circle extends Shape{
private static final double PI = Math.PI;//圆周率,可以替换为3.14的按照需求啦
private double r ;//半径
public Circle(double r){
this.r = r;
}
public double getArea() {
return PI * r *r;
}
}
-----------------测试结果
长3宽5的长方形面积是:15.0
半径为3的圆的面积为:28.274333882308138
展开全部
abstract class Shape{
abstract double GetArea();
}
public class Rectangle extends Shape{
private double len,wid;
public double GetArea(){return len*wid;}
}
public class Circle extends Shape{
private double r;
public double GetArea(){ return r*r*3.14;}
}
abstract double GetArea();
}
public class Rectangle extends Shape{
private double len,wid;
public double GetArea(){return len*wid;}
}
public class Circle extends Shape{
private double r;
public double GetArea(){ return r*r*3.14;}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
abstract public class Shape
{
public Shape()
{
}
abstract public double GetArea();
}
public classCircle extends Shape
{
public double GetArea()
{
double are = 计算面积;
return are;
}
}
public class Rectangle extends Shape
{
public double GetArea()
{
double are = 计算面积;
return are;
}
}
{
public Shape()
{
}
abstract public double GetArea();
}
public classCircle extends Shape
{
public double GetArea()
{
double are = 计算面积;
return are;
}
}
public class Rectangle extends Shape
{
public double GetArea()
{
double are = 计算面积;
return are;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询