Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car
- {
- private int num;
- private double gas;
- public Car()
- {
- num = 0;
- gas = 0.0;
- System.out.println("生產了汽車。");
- }
- public void setCar(int n, double g)
- {
- num = n;
- gas = g;
- System.out.println("已使車號為" + num + ",使汽油量為" + gas + "。");
- }
- public void show()
- {
- System.out.println("車號是" + num + "。");
- System.out.println("汽油量是" + gas + "。");
- }
- }
- public class HelloWorld
- {
- public static void main(String args[])
- {
- Car cars[] = new Car[3];
- for(int i=0; i<cars.length; i++)
- {
- cars[i] = new Car();
- }
- cars[0].setCar(1234, 20.5);
- cars[1].setCar(2345, 30.5);
- cars[2].setCar(3456, 40.5);
- for(int i=0; i<cars.length; i++)
- {
- cars[i].show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment