Definition and use of abstract classes
An abstract class cannot be instantiated, but an array type of abstract class name can be, see case
1 Packagecom.example;2 3 4 Public classShapetest {5 Public Static voidMain (string[] args) {6shape[] Shapes =NewSHAPE[3];//shape is an abstract class and cannot be instantiated; shape[] An array class that can be instantiated7Shapes[0] =NewSquare (3.1);8SHAPES[1] =NewCircle (1.5);9SHAPES[2] =NewCircle (2.4);TenShapetest x =Newshapetest (); One System.out.println (X.areamax (shapes)); A } - - DoubleAreamax (shape[] shapes) { the DoubleAreamax = Shapes[0].area (); - for(inti=0;i<shapes.length;i++){ - DoubleMax =Shapes[i].area (); - if(max>Areamax) { +Areamax =Max; - } + } A returnAreamax; at } - } - - Abstract classshape{ - DoubleA; - Abstract DoubleArea (); in } - to //subclass Inherits Parent class abstract method--rewrite + classSquareextendsshape{ -Square (Doublea) { the This. A =A; * } $ }Panax Notoginseng @Override - DoubleArea () { the return0.0625*a*A; + } A } the + classCircleextendsshape{ -Circle (Doublea) { $ This. A =A; $ } - @Override - DoubleArea () { the return0.0796*a*A; - }Wuyi}
Java-day12-shapetest