Lesson_7 作业_1 Driver 和 Car

一.作业描述

  定义一个司机类Driver,有属性为 年龄、驾龄、工资;方法:开车的方法driverCar ,该方法应该可以驾驶各种交通工具。定义几种交通工具类如:Bus、Car、Motor、Plane等,给这些交通工具定义属性有:name、speed等,提供一个显示当前状态的方法(打印当前工具的速度等信息即可),
编写一个类测试上述类的使用。

 

二.代码

 1 /***********************************************************
 2 *                 Lesson_7 作业_1 -- Driver & Car
 3 *                         2013-01-17
 4 *                        by CocoonFan
 5 *
 6 ************************************************************
 7 *************************作业描述***************************
 8 *
 9 *       定义一个司机类Driver,有属性为 年龄、驾龄、工资;
10 * 方法:开车的方法driverCar ,该方法应该可以驾驶各种交通工具。
11 * 定义几种交通工具类如:Bus、Car、Motor、Plane等,给这些交通
12 * 工具定义属性有:name、speed等,提供一个显示当前状态的方法(
13 * 打印当前工具的速度等信息即可),编写一个类测试上述类的使用。
14 ************************************************************/
15 
16 public class TestDriver{
17     public static void main(String []args){
18         Driver zhangsan = new Driver("张三", 40, 20, 3000.00);
19         Driver lisi = new Driver("李四", 35, 8, 8000.00);
20         Driver wangwu = new Driver("王五", 20, 2, 2000.00);
21         Driver chenliu = new Driver("陈六", 30, 5, 15000.00);
22 
23         Car bus = new Car("Bus", 40);
24         Car car = new Car("Car", 80);
25         Car motor = new Car("Motor", 100);
26         Car plane = new Car("Plane", 2100);
27 
28         zhangsan.driverCar(bus);
29         lisi.driverCar(car);
30         wangwu.driverCar(motor);
31         chenliu.driverCar(plane);
32     }
33 }
34 
35 class Driver{
36     private String name;
37     private int age;
38     private int driveAge;
39     private double salary;
40 
41     //构造方法
42     public Driver(String name, int age, int driveAge, double salary){
43         this.name = name;
44         this.age = age;
45         this.driveAge = driveAge;
46         this.salary = salary;
47     }
48 
49     public void showDriver(){
50         System.out.println("\n姓名:" + name);
51         System.out.println("年龄:" + age + "年");
52         System.out.println("驾龄:" + driveAge + "年");
53         System.out.println("收入:" + salary + "元人民币");
54     }
55 
56     //driverCar方法
57     public void driverCar(Car car){
58         showDriver();
59         System.out.println(name + "正在驾驶车.");
60         car.showCarCondition();
61     }
62 }
63 
64 class Car{
65     private String kind;
66     private double speed;
67 
68     //构造方法
69     public Car(String kind, double speed){
70         this.kind = kind;
71         this.speed = speed;
72     }
73 
74     public void showCarCondition(){
75         System.out.println("车的类型为:" + this.kind);
76         System.out.println("车的速度为:" + this.speed + " km/h");
77     }
78 }

三.运行结果

原文地址:https://www.cnblogs.com/CocoonFan/p/2865527.html