问答详情
源自:10-3 Java 中的抽象类

作业分享,一起学习。

package com.imooc;

public abstract class Shape {

public abstract double ZhouChang();

public abstract double MianJi();

}


package com.imooc;


public class Rectangle extends Shape {

static int Chang;

static int Kuan;


@Override

public double ZhouChang() {

// TODO Auto-generated method stub


return (Chang + Kuan) * 2;


}


@Override

public double MianJi() {

// TODO Auto-generated method stub

return Chang * Kuan;

}


}

package com.imooc;


public class Circle extends Shape {

static int r;

@Override

public double ZhouChang() {

// TODO Auto-generated method stub

return 2*Math.PI*r;

}


@Override

public double MianJi() {

// TODO Auto-generated method stub

return Math.PI*r*r;

}

}

package com.imooc;


import java.util.Scanner;


public class Test2 {

@SuppressWarnings("resource")

public static void main(String[] args) {

Shape r1=new Rectangle();

Scanner s = new Scanner(System.in);

System.out.println("请分别输入长方形的长和宽:");

Rectangle.Chang = s.nextInt();

Rectangle.Kuan=s.nextInt();

System.out.println("长方形周长为: "+r1.ZhouChang());

System.out.println("长方形面积为: "+r1.MianJi());

Shape r2=new Circle();

System.out.println("请输入圆的半径");

Circle.r=s.nextInt();

System.out.println("圆的周长是:"+r2.ZhouChang());

System.out.println("圆的面积是:"+r2.MianJi());


}

}

https://img2.mukewang.com/5b82918600018fb305090388.jpg

提问者:慕梓981030 2018-08-26 19:40

个回答

  • qq_追逐梦想_4
    2018-08-26 20:29:24
    已采纳

    光明正大的炫技,鄙视你!!!

  • 人间最美鲸落
    2018-12-04 19:28:28

    为什么前边的长宽必须要是静态变量


  • 晨旭圆
    2018-10-01 14:52:27

    厉害厉害

  • 慕粉4401973
    2018-09-28 21:52:25

    太棒了,谢谢!学到了