- abstract class Shap {//抽象类
- protected double width;
- protected double length;
- Shap(final double width, final double length) {
- this.width = width;
- this.length = length;
- }
- abstract double area();//抽象方法
- }
- class Square extends Shap {
- Square(final double width, final double length) {
- super(width, length);
- }
- double area() {
- return width * length;
- }
- }
- class Triangle extends Shap {
- Triangle(final double width, final double length) {
- super(width, length);
- }
- double area() {
- return width*length/2;
- }
- }
- public class CalculateArea {
- public static void main(String[] args) {
- Square square = new Square(20,10);
- System.out.println("\n正方形的面积为:" + square.area());
- Triangle triangle = new Triangle(20,10);
- System.out.println("\n三角形的面积为:" + triangle.area());
- }
- }