作业分享,一起学习。
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());
}
}