Guest User

Untitled

a guest
Sep 17th, 2020
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. class Car
  2. {
  3.     private int num;
  4.     private double gas;
  5.  
  6.     public Car()
  7.     {
  8.         num = 0;
  9.         gas = 0.0;
  10.         System.out.println("生產了汽車。");
  11.     }
  12.  
  13.     public void setCar(int n, double g)
  14.     {
  15.         num = n;
  16.         gas = g;
  17.         System.out.println("已使車號為" + num + ",使汽油量為" + gas + "。");
  18.     }
  19.  
  20.     public void show()
  21.     {
  22.         System.out.println("車號是" + num + "。");
  23.         System.out.println("汽油量是" + gas + "。");
  24.     }
  25. }
  26.  
  27. public class HelloWorld
  28. {
  29.     public static void main(String args[])
  30.     {
  31.         Car cars[] = new Car[3];
  32.         for(int i=0; i<cars.length; i++)
  33.         {
  34.             cars[i] = new Car();
  35.         }
  36.  
  37.         cars[0].setCar(1234, 20.5);
  38.         cars[1].setCar(2345, 30.5);
  39.         cars[2].setCar(3456, 40.5);
  40.  
  41.         for(int i=0; i<cars.length; i++)
  42.         {
  43.             cars[i].show();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment