publicclassTestDriver{publicstaticvoidmain(String[] args){// 创建司机对象(外部类对象)Driver driver =newDriver();// 设置司机对象属性
driver.setName("唐小刚");// 基于司机对象(外部类对象)创建奔驰对象(内部类对象)Driver.Benz benz = driver.newBenz();// 设置奔驰对象属性
benz.setModel("梅赛德斯-AMG A 35 L 4MATIC");
benz.setPrice(45);
benz.setColor("蓝");// 调用司机对象的驾驶方法
driver.drive(benz);System.out.println("=======================");// 司机驾驶宝马车(采用匿名内部类方式)
driver.drive(newCar(){privateString model ="全新BMW Z4敞篷跑车";privatedouble price =55;privateString color ="红";@Overridepublicvoidstart(){System.out.println("宝马["+ model +"]起步……");}@Overridepublicvoidrun(){System.out.println("宝马["+ model +"]行驶……");}@Overridepublicvoidstop(){System.out.println("宝马["+ model +"]停止……");}publicvoidspeak(){System.out.println(color +"色宝马["+ model +"]单价:"+ price +"万~");}});}}