-1

如果继承 TrainCar 这是一个问题。如果继承 GCompoud 那很好。哪里错了?

家长:</p>

public class TrainCar extends GCompound{

    public TrainCar(double size){

        engine =  new Engine(size);
        add(engine);
        }
}

子类:</p>

public class Engine extends  TrainCar { 

    public Engine (double size){    //if inherit TrainCar that's a problem. 
                                    //if inherit GCompoud that's well.

        GPolygon engine = engine(size);     
        add(engine);
    }
}

Boxcar 继承了 TrainCar,这很好。

在讲义 #31 中:

public class Boxcar extends TrainCar {

 public Boxcar(Color color) {
}

http://www-cs-faculty.stanford.edu/~eroberts/courses/cs106a/handouts/30-graphical-structures.pdf

4

1 回答 1

0

您必须TrainCar在 的构造函数中调用构造函数Engine。你可以这样做:

public Engine (double size) {
     super(size);

    GPolygon engine = engine(size);     
    add(engine);
}
于 2013-03-02T11:15:39.360 回答