现在的位置: 首页 > 综合 > 正文

【年少的风】java测试抽象类,继承

2017年10月10日 ⁄ 综合 ⁄ 共 1038字 ⁄ 字号 评论关闭

 

  1. abstract class Shap {//抽象类  
  2.  
  3.     protected double width;  
  4.     protected double length;  
  5.       
  6.     Shap(final double width, final double length) {  
  7.         this.width = width;  
  8.         this.length = length;  
  9.     }  
  10.       
  11.     abstract double area();//抽象方法  
  12. }  
  13.  
  14. class Square extends Shap {  
  15.  
  16.     Square(final double width, final double length) {  
  17.         super(width, length);  
  18.     }  
  19.       
  20.     double area() {  
  21.         return width * length;  
  22.     }  
  23. }  
  24.  
  25. class Triangle extends Shap {  
  26.  
  27.     Triangle(final double width, final double length) {  
  28.         super(width, length);  
  29.     }  
  30.       
  31.     double area() {  
  32.         return width*length/2;  
  33.     }  
  34. }  
  35.  
  36. public class CalculateArea {  
  37.       
  38.     public static void main(String[] args) {  
  39.       
  40.         Square square = new Square(20,10);  
  41.         System.out.println("\n正方形的面积为:" + square.area());  
  42.           
  43.         Triangle triangle = new Triangle(20,10);  
  44.         System.out.println("\n三角形的面积为:" + triangle.area());  
  45.           
  46.           
  47.           
  48.           
  49.     }  
  50.       

 

本文出自 “年少的风” 博客,请务必保留此出处http://huamm.blog.51cto.com/5646020/1047173

抱歉!评论已关闭.