写的很规范,值得参考。
/*Shape类*/
package com.abstract2;
public abstract class Shape {
public abstract void calarea();
public abstract void calperimeter();
}
/*Rectangle类*/
package com.abstract2;
public class Rectangle extends Shape {
public double h ;
public double w;
public Rectangle(double h,double w) {
this.h = h;
this.w = w;
}
@Override
public void calarea() {
// TODO Auto-generated method stub
System.out.println("矩形的area:"+h*w);
}
@Override
public void calperimeter() {
// TODO Auto-generated method stub
System.out.println("矩形的perimeter:"+2*(h+w));
}
}
/*circle类*/
package com.abstract2;
public class circle extends Shape {
public double r ;
public circle(double r) {
this.r = r;
}
@Override
public void calarea() {
// TODO Auto-generated method stub
System.out.println("圆的area:"+Math.PI*r*r);
}
@Override
public void calperimeter() {
// TODO Auto-generated method stub
System.out.println("圆的perimeter:"+Math.PI*2*r);
}
}
/*测试类*/
package com.abstract2;
public class initial {
public static void main(String[] args) {
Shape s1 = new Rectangle (8,6) {
};
s1.calarea();
s1.calperimeter();
Shape s2 = new circle (2);
s2.calarea();
s2.calperimeter();
}
}