package com.yong; public abstract class Shape { public abstract void Perimeter(); public abstract void Area(); }
package com.yong;
public class Rectangle extends Shape {
double a=10;
double b=20;
@Override
public void Perimeter() {
double c=(a+b)*2;
System.out.println("长方形的周长为:"+c);
}
@Override
public void Area() {
double s=a*b;
System.out.println("长方形的面积为:"+s);
}
}
package com.yong;
public class Circle extends Shape {
final double PI=3.14;
double r=10.0;
@Override
public void Perimeter() {
double c=2*PI*r;
System.out.println("圆的周长为:"+c);
}
@Override
public void Area() {
double s=PI*r*r;
System.out.println("圆的面积为:"+s);
}
}
package com.yong;
public class Test {
public static void main(String[] args) {
Shape sh1=new Rectangle();
sh1.Area();
sh1.Perimeter();
Shape sh2=new Circle();
sh2.Perimeter();
sh2.Area();
}
}
长方形的面积为:200.0
长方形的周长为:60.0
圆的周长为:62.800000000000004
圆的面积为:314.0
System.out.println("圆的周长为:"+c);
System.out.println("圆的面积为:"+s);
这些都能写到父类里面吗,为什么?求指导
改为 System.out.println("圆的周长为:"+String.format("%.1f", c));
位数对
输入的数算圆面积和长方形的周长面积是个整数,但是定义的是double型的,所以后面有0补位;但是圆的周长不是整数,不用补位。