码迷,mamicode.com
首页 > 其他好文 > 详细

上机代码

时间:2019-05-24 00:33:10      阅读:107      评论:0      收藏:0      [点我收藏+]

标签: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 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!