package Abstrace;
import Abstrace.Rectangle;
import Abstrace.Shape;
import Abstrace.Triangle;
//定义一个抽象类
abstract class Shape{
public abstract void area();//定义一个抽象方法
}
class Rectangle extends Shape{//矩形类
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void area() {
// TODO Auto-generated method stub
double ss =(height*width);
System.out.println("所求矩形面积为"+ss);
}
}
class Circle extends Shape{//定义一个圆形类
private double r;
public static final double PI = 3.14;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void area() {
// TODO Auto-generated method stub
double ss =PI *r*r;
System.out.println("所求矩形面积为"+ss);
}
}
class Triangle extends Shape{//定义一个三角形类
public double a;
public double b;
public double c;
//添加一个构造方法
public Triangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
//重写抽象方法
public void area() {
// TODO Auto-generated method stub
double s1=(a+b+c)/2;
double s2 = s1*(s1-a)*(s1-b)*(s1-c);
double result = Math.sqrt(s2);
System.out.println("所求矩形面积为"+result);
}
}
public class ShapeDemo {
public static void main (String [] args){
Shape rectangle = new Rectangle(8.0,7.0);
Shape circle = new Circle(1.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
rectangle.area();
circle.area();
triangle.area();
}
}