本帖最后由 陈迎春 于 2013-3-17 09:49 编辑
- package cn.njit.extendsdemo;
- abstract class Shape//定义抽象类
- {
- public abstract double getArea(/*Shape shape*/);
- }
- class Rectangle extends Shape//定义矩形继承shape
- {
- private static double length,width;
- public Rectangle(double length,double width)
- {
- Rectangle.length = length;
- Rectangle.width = width;
- }
- public double getArea(/*Rectangle rectangle*/)
- {
- return Rectangle.length*Rectangle.width;
- }
- }
- class Square extends Rectangle//定义长方形继承Rectangle
- {
- //private double length,width;
- public Square(double length) {
- super(length, length);
- // TODO 自动生成的构造函数存根
- }
-
- }
- public class AbstractDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- Square s = new Square(5);
- s.getArea();//求长方形的面积。为什么调用这个方法求不出结果呢?
- }
- }
复制代码 |