fork download
  1.  
  2. class Car{
  3. private int num;
  4. private double gas;
  5.  
  6. public Car(){
  7. num = 0;
  8. gas = 0.0;
  9. System.out.println("車を作成しました。");
  10. }
  11.  
  12. public Car(int n,double g){
  13. num = n;
  14. gas = g;
  15. System.out.println("ナンバーを" + num +"、ガソリン量が" + gas + "の車を作成しました");
  16. }
  17.  
  18. public void setCar(int n,double g){
  19. num = n;
  20. gas = g;
  21. System.out.println("ナンバーを" + num +"、ガソリン量が" + gas + "の車を作成しました");
  22. }
  23.  
  24. public void show(){
  25. System.out.println("ナンバー:" + num);
  26. System.out.println("ガソリン量:" + gas);
  27. }
  28. }
  29. //レーシングカークラス
  30. class RacingCar extends Car{
  31. private int course;
  32. public RacingCar(){
  33. course = 0;
  34. System.out.println("レーシングカーを作成しました。");
  35. }
  36.  
  37. public RacingCar(int n,double g,int c){
  38. super(n,g); //スーパークラスの引数二つを呼び出し
  39. course = c;
  40. System.out.println("コース番号:" + course + "のレーシングカーを作成しました");
  41. }
  42. public void setCourse(int c){
  43. course = c;
  44. System.out.println("コース番号:" + course);
  45. }
  46. }
  47.  
  48. class sample{
  49. public static void main (String[] args){
  50. //サブクラスの3つのコンストラクタを呼び出し
  51. RacingCar rccar1 = new RacingCar(1234,20.5,5);
  52. }
  53. }
Success #stdin #stdout 0.18s 58116KB
stdin
Standard input is empty
stdout
ナンバーを1234、ガソリン量が20.5の車を作成しました
コース番号:5のレーシングカーを作成しました