# 日常练习之抽象类

2017-10-11 19:19:58来源:CSDN作者:zhuoshengda人点击

### 题目

``package xml;import java.util.Scanner;/** * 描述: * * @outhor 71948 * @create 2017-10-11 16:26 */abstract class Shape {    public abstract double area();// 面积    public abstract double zhouchang();// 周长}/** 这是求圆的* 面积求法为：PI*R^2* 使用Math包导入* 只需要一个数值半径即可* */class Circle extends Shape {    private double r;    public Circle(double r) {        this.r = r;    }    public double area() {        return Math.PI * r * r;    }    @Override    public double zhouchang() {        return 2 * Math.PI * r;    }}/** 这是求三角形的* 面积计算：* 二分之（底边+高）* 周长为两倍底边加高* */class Tangle extends Shape {    private double height, width;    public Tangle(double height, double width) {        this.height = height;        this.width = width;    }@Overridepublic double area() {    return this.height * width;}@Overridepublic double zhouchang() {    return 2 * (this.height + this.width);}}public class Test {    public static void main(String[] args) {        System.out.println("请输入圆的半径：");        Scanner sca = new Scanner(System.in);    Shape shape1 = new Circle(sca.nextInt());    System.out.println("圆的面积为：" + shape1.area());    System.out.println("圆的周长为：" + shape1.zhouchang());    System.out.println("请输入三角形的底边跟高：");    Shape shape2 = new Tangle(sca.nextInt(),sca.nextInt());    System.out.println("三角形的面积为：" +shape2.area());    System.out.println("三角形的面积为：" +shape2.zhouchang());    }}``