スーパークラスにて設定したメンバをサブクラスから呼び出し表示させたいのですが、どうもうまくいきません。(下記の処理では、メインクラス→サブクラスのpublicメソッド内にあるsuper.メンバ変数)
どなたかヒントを頂けませんでしょうか。
よろしくお願いします。
lang
1// メインクラス 2class Sample 3{ 4 public static void main(String[] args) 5 { 6 7 Car car = new Car(); 8 car.setCar(1,1.1); 9 car.show(); 10 11 RacingCar racingCar = new RacingCar(); 12 racingCar.showSub(); // サブクラスのメソッドにアクセス 13 14 } 15} 16 17 //スーパークラス 18 class Car 19 { 20 21 protected int num; 22 protected double gas; 23 24 public Car() 25 { 26 this.num = 0; 27 this.gas = 0.0; 28 } 29 public Car(String str) 30 { 31 System.out.println(str); 32 } 33 public void setCar (int n , double g) 34 { 35 this.num = n; 36 this.gas = g; 37 } 38 39 public void show () 40 { 41 System.out.println("スーパークラス/ナンバー:" +this.num + " ガソリン:" + this.gas + "/ "); 42 } 43 44} 45 46 // サブクラス 47class RacingCar extends Car 48{ 49 protected int course; 50 51 public RacingCar () 52 { 53 super("コンストラクタのオーバーロードで初期値になることを避けた"); 54 this.course = 0; 55 } 56 57 public void showSub () 58 { 59 System.out.println("サブクラス/ナンバー:" + super.num + " ガソリン:" + super.gas + "コース:" + this.course + "/ "); 60 } 61} 62 63 64
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/05/09 14:16
2015/05/09 14:25
2015/05/09 14:33