package 抽象类运用;
abstract class MyShape{ String name; public MyShape(String name) { this.name = name; } public abstract void getArea(); public abstract void getlength(); } class Circla extends MyShape{ double r; public static final double pi = 3.14; public Circla(String name,double r) { super(name); this.r=r; } public void getArea() { System.out.println(name+"面积为:"+pi*r*r); } public void getlength() { System.out.println(name+"周长为:"+2*pi*r); } } class Rect extends MyShape{ int width; int higth; public Rect(String name,int width,int higth) { super(name); this.width = width; this.higth = higth; } public void getArea() { System.out.println(name+"的面积为:"+width*higth); } public void getlength() { System.out.println(name+"的周长为:"+2*width+2*higth); } }
public class Dome { public static void main(String [] args) { Circla c1 = new Circla("圆",4); System.out.println("图形的名字叫做:"+c1.name); c1.getArea(); c1.getlength(); Rect r1 = new Rect("矩形",2,3); System.out.println("图形的名字叫做:"+r1.name); r1.getArea(); r1.getlength(); } }