抽象类和继承类的创建

计算由几个不同形状的图形组成的总面积。设要计算的三角形(triangle)、圆(circle)和矩形(rectangle)的面积之和。

1.定义Shape的抽象类,再定义getArea的抽象方法;
2.定义triangle,circle,rectangle类继承Shape,重写getArea方法即可。


public class 继承测试 {
    /*
     * 在该类中定义两个方法,一个是 getName,用于使用反射机制获得类名称;另一个是抽象方法 getArea ,用来计算图形的面积。 (2)创建圆形类
     * Circle ,继承自 Shape ,并实现抽象方法getArea。 在 Circle
     * 类的构造方法中获得了圆形的半径,用于在getArea计算圆形的面积。 (3)创建矩形类 Rectangle ,继承自 Shape , 并实现抽象方法
     * getArea 。在 Rectangle 类的构造方法中获得了矩形的长和宽,用于在 getArea计算矩形的面积。
     * 
     */
    public static void main(String[] args) {
        Shape circle = new Circle(10);
        System.out.println("类名称是:" + circle.getName());
        float s = circle.getArea();
        System.out.println("圆的面积=" + circle.getArea());
        Shape rect = new Rectangle(10, 20);
        System.out.println("类名称是:" + rect.getName());
        System.out.println("矩形的面积=" + rect.getArea());
        s += rect.getArea();
        Shape tri = new Triangle(3, 4, 5);
        System.out.println("类名称是:" + tri.getName());
        System.out.println("三角形的面积=" + tri.getArea());
        s += tri.getArea();
        
        System.out.println("总面积:"+s);
        

    }
}

abstract class Shape {
    String getName() {
        return this.getClass().getName();
    }

    abstract float getArea();
}

class Circle extends Shape {

    float r;

    public Circle() {
    };

    public Circle(float r) {
        this.r = r;
    }

    @Override
    float getArea() {

        return 3.14f * r * r;
    }

}

class Rectangle extends Shape {

    float width;
    float height;

    public Rectangle() {
    }

    public Rectangle(float width, float height) {
        this.width = width;
        this.height = height;
    }

    @Override
    float getArea() {

        return width * height;
    }

}

class Triangle extends Shape // 三角形类
{
    public Triangle() {
    }

    private float sideA, sideB, sideC;
    private boolean boo;

    public Triangle(float a, float b, float c) {
        sideA = a;
        sideB = b;
        sideC = c;
        if (a + b > c && a + c > b && b + c > a) {
            System.out.println("我是一个三角形");
            boo = true;
        } else {
            System.out.println("我不是一个三角形");
            boo = false;
        }
    }

    public float getArea() {
        float area;
        float p = (sideA + sideB + sideC) / 2.0f;
        area = (float)Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
        System.out.println("三角形面积是:" + area);
        return area;
    }

}

Shape.java文件:

abstract class shape
{
       String name;
       abstract void Area();
}
    class Trangle extends shape  //三角形类
    {  public Trangle(){}
       private double sideA,sideB,sideC;
       private boolean boo;
       public Trangle(double a,double b,double c)
       {
          sideA=a;sideB=b;sideC=c;
          this.name="三角形";
          if(a+b>c&&a+c>b&&b+c>a)
          {
           System.out.println("我是一个三角形");
           boo=true;
          }
          else
          {
           System.out.println("我不是一个三角形");
           boo=false;
          }
       }
       public void Area()
       {
          if(boo)   
          {
             double p=(sideA+sideB+sideC)/2.0;
             double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
             System.out.println(name+"面积是:"+area);
          }
          else
          {
             System.out.println("不是一个三角形,不能计算面积");
          }
       }
       
    }
    class Circle extends shape   //圆类
    {
       double r;
       Circle(double r)
       {
          this.r=r;this.name="圆";
       }
       public void Area()
       {
          System.out.println(name+"面积是:"+3.14*r*r);
       }
    }
    class Rectangle extends shape   //矩形类
    {
        double a,b;
        Rectangle(double a,double b)
        {
           this.a=a;this.b=b;this.name="矩形";
        }
        public void Area()
        {
           System.out.println(name+"面积是:"+a*b);
        }
    }

Test.java文件:



public class Test2 {

    public static void main(String[] args)
       {
          Trangle t=new Trangle(1,2,3);
          t.Area();
         
          t.Area();
          
          Circle c=new Circle(2);
          c.Area();
          
          Rectangle r=new Rectangle(4,5);
          r.Area();
       }

}