上机代码

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 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();
   }
}

 

posted on 2019-05-24 00:23  18软工五班罗小杨  阅读(212)  评论(0编辑  收藏  举报