abstract class Shape{
abstract double getArea()//抽象的求面积方法
}
//矩形铅信类槐颂轮
class Rectangle extends Shape{
protected double width
protected double height
public Rectangle(double width, double height){
this.width = width
this.height = height
}
@Override
double getArea() {//实现父类的方法
return this.width * this.height
}
}
//椭圆类
class Ellipse extends Shape{
protected double a
protected double b
public Ellipse(double a, double b){
this.a = a
this.b = b
}
@Override
double getArea() {
return Math.PI * this.a * this.b
}
}
public class TestAbstract {
public static void main(String[] args) {
Shape s
s = new Rectangle(3, 4)
System.out.println("矩形的樱闷面积 : " + s.getArea())
s = new Ellipse(4, 3)
System.out.println("椭圆的面积 : " + s.getArea())
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)