标签:abstract sys dem void public out circle ati length
abstract class Shape{ String name; public Shape(String name) { this.name=name; } abstract void Length(); abstract void Area(); } class Circle extends Shape{ double pi=3.14; int r; public Circle(String name,int r) { super(name); this.r=r; } public void Area(){ System.out.println(name+"的面积为:"+r*r*pi); } public void Length() { System.out.println(name+"的周长为:"+r*2*pi); } } class Rect extends Shape{ int width; int higth; public Rect(String name,int width,int higth) { super(name); this.width=width; this.higth=higth; } public void Area(){ System.out.println(name+"的面积为:"+width*higth); } public void Length() { System.out.println(name+"的周长为:"+(width+higth)*2); } } class Square extends Shape{ double width; public Square(String name,double width) { super(name); this.width=width; } public void Area(){ System.out.println(name+"的面积为:"+width*width); } public void Length() { System.out.println(name+"的周长为:"+4*width); } } public class Demo1 { public static void main(String [] args) { Shape c =new Circle("圆",4); print(c); Shape r = new Rect("矩形",5,6); print(r); Shape s =new Square("正方形",6); print(s); } public static void print(Shape t) { t.Length(); t.Area(); } }
标签:abstract sys dem void public out circle ati length
原文地址:https://www.cnblogs.com/0929-luoyang/p/10915485.html