class Engine { // エンジンを表すクラス // 0.1秒間スロットルをt%開ける(t=100.0が全開) public double throttle( double t ) { double power = 0.0; // 詳細は略。パワーを値として返す。 return power; } } // 誤って,CarをEngineのサブクラスにして定義している class Car extends Engine { // 車を表すクラス // 車の位置情報 private double x = 0.0, y = 0.0; Car( double x, double y ) { this.x = x; this.y = y; } // 0.s秒間エンジンのスロットルをt%開ける void move( int s, double t ) { double power = 0.0; for( int i = 0; i < s; i++ ) { power = throttle( t ); // 以下,0.1秒間エンジンから供給されるパワーで // 車を進める。 } } } class BadIsA { public static void main( String args[] ) { Car c = new Car( 0.0, 0.0 ); // 車オブジェクトを生成する c.move( 10, 10.0 ); // 1秒間スロットルを10%開けて移動する Engine e = c; // “Car is a Engine”という不正な関係 e.throttle( 100.0 ); // cのエンジンが勝手にいじられてしまう } }