美文网首页
2018-12-23

2018-12-23

作者: Dream__catcher | 来源:发表于2018-12-23 20:47 被阅读0次
    import java.util.*;
    import java.util.Scanner;
    abstract class Shape{
        final double PI = 3.14;//不可变静态常量
        abstract public double getPerimeter();
        abstract public double getArea();
    }
    class Rectangle extends Shape{
        public int w,l;
        Rectangle(int width,int length){
            this.w = width;
            this.l = length;
        }
        public double getPerimeter(){
            return  (w+l)*2;
        }
        public double getArea(){
            return w*l;
        }
    
    }
    class Circle extends Shape{
            public int r;
            Circle(int radius){
                this.r = radius;
            }   
            public double getPerimeter(){
                return  2*r*PI;
            }
            public double getArea(){
                return r*r*PI;
            }
    
        }
    public class Main1 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            //System.out.println("请输入整数:");,去掉
            int n = in.nextInt();
            Shape[] s = new Shape[n];
           
            for(int i=0;i<n;i++){
                String str = in.next();
                //去掉Shape shape;//声明一个shape对象
                if(str.equals("rect")) {
                    int w = in.nextInt();
                    int l = in.nextInt();
           
                    s[i] = new Rectangle(w,l);
                    
                }else {
                    int r = in.nextInt();
                    s[i]= new Circle(r);
                }
                //去掉s[i] = shape;//把声明的对象放入形状数组
            }
            System.out.println(sumAllPerimeter(s));
            System.out.println(sumAllArea(s));
            System.out.print("[");
            for(int j=0;j<n;j++) {
                
                if(s[j].getClass()==Rectangle.class)
                    System.out.print("Rectangle [width="+((Rectangle)s[j]).w+", length="+((Rectangle)s[j]).l+"]");//((Rectangle)s[j])强制类型转换
                if(s[j].getClass()==Circle.class){
                    System.out.print("Circle [radius="+((Circle)s[j]).r+"]");
                }
                if(j!=n-1) {
                    System.out.print(", ");
                }else {
                    System.out.println("]");
                    //System.out.println();ln代表输出后换行
                }
            }
            for(int i=0;i<n;i++) {
                System.out.print(s[i].getClass()+",");
                System.out.println(s[i].getClass().getSuperclass());
            }
            
            
        }
        static double sumAllArea(Shape[] s){//静态函数(Main)只能调用静态函数(sumAllArea(Shape[] s))
            double sum=0;
            for(int i=0;i<s.length;i++) {
                Shape shape;
                shape = s[i];
                double d = shape.getArea();
                sum = sum+d;
            }
            return sum;
        }
        static double sumAllPerimeter(Shape[] s){
            double sum=0;
            for(int i=0;i<s.length;i++) {
                Shape shape;
                shape = s[i];
                double d = shape.getPerimeter();
                sum = sum+d;
            }
            return sum;
    }
    }
    

    相关文章

      网友评论

          本文标题:2018-12-23

          本文链接:https://www.haomeiwen.com/subject/qycokqtx.html