java - day12 - ShapeTest

來源:互聯網
上載者:User

標籤:example   shape   抽象類別   類型   stat   子類   oid   ++   重寫   

  抽象類別的定義及使用

     抽象類別不能執行個體化,但抽象類別名的數群組類型可以,見案例

 1 package com.example; 2  3  4 public class ShapeTest { 5     public static void main(String[] args){ 6         Shape[] shapes = new Shape[3];  //shape為抽象類別,不可以執行個體化;shape[]為數組類,可以執行個體化 7         shapes[0] = new Square(3.1); 8         shapes[1] = new Circle(1.5); 9         shapes[2] = new Circle(2.4);10         ShapeTest x = new ShapeTest();11         System.out.println(x.areaMax(shapes));12     }13 14     double areaMax(Shape[] shapes){15         double areamax = shapes[0].area();16         for(int i=0;i<shapes.length;i++){17             double max = shapes[i].area();18             if(max>areamax){19                  areamax = max;20             }21         }22         return areamax;23     }24 }25 26 abstract class Shape{27     double a;28     abstract double area();29 }30 31 //子類繼承父類抽象方法--重寫32 class Square extends Shape{33     Square(double a){34             this.a = a;35         }36     }37     @Override38     double area(){39         return 0.0625*a*a;40     }41 }42 43 class Circle extends Shape{44     Circle(double a){45         this.a = a;46     }47     @Override48     double area(){49         return 0.0796*a*a;50     }51 }

 

java - day12 - ShapeTest

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.