public class TextAbstract {
public static void main(String[] args) {
Rect len = new Rect(2, 3);
double L = len.getLen();
double W = len.getArea();
System.out.println(L + " " + W);
Circle a = new Circle(5);
double L1 = a.getArea();
double W1 = a.getLen();
System.out.println(L1 + " " + W1);
}
}
/*抽象类
* 提供统一的长度和面积的计算方式:为了统一
* 在父类中用抽象方法进行定义
*/
abstract class MyShape {
abstract double getLen();
abstract double getArea();
}
class Rect extends MyShape {
double width;
double height;
Rect() {
}
Rect(double width, double height) {
this.height = height;
this.width = width;
}
@Override
double getLen() {
return 2 * (height + width);
}
@Override
double getArea() {
return height * width;
}
}
class Circle extends MyShape {
double r;
double PI = 3.14;
Circle() {
}
Circle(double r) {
this.r = r;
}
@Override
double getLen() {
return 2 * PI * r;
}
@Override
double getArea() {
return PI * r * r;
}
}