用继承和组合的知识构造一辆汽车,功能需求见注释
package com.Summer_0425.cn; /** * @author Summer * 需求分析: * 发动机Engine:启动start 停止stop * 轮子Wheel:充气inflate(int 容量) * 车窗Window:开roolUp 降roolDown * 车门Door: 开open 关close 门上有窗 * 汽车Car: * 有一个发动机 * 4个轮子 * 2个门 */ //发动机 class Engine{ public void start(){System.out.println("发动机启动");} public void stop(){System.out.println("发动机停止");} } //轮子 class Wheel{ public void inflate(int num){System.out.println("给车轮充气"+num);} } //车窗 class Window{ public void roolUp(){System.out.println("车窗升");} public void roolDown(){System.out.println("车窗降");} } //车门 class Door{ Window win = new Window();//车门上有窗户,使用组合的方式 public void start(){System.out.println("发动机启动");} public void stop(){System.out.println("发动机停止");} public void open() { // TODO Auto-generated method stub } public void close() { // TODO Auto-generated method stub } } class Car{ Engine engine;//使用组合的方式 Wheel [] wheel; Door [] door; Car(){ engine = new Engine(); wheel = new Wheel[4]; for (int i = 0; i < wheel.length; i++) { wheel[i] = new Wheel(); } door = new Door[2]; for (int i = 0; i < door.length; i++) { door[i] = new Door(); } } } public class Tset02 { public static void main(String[] args) { Car car = new Car(); for (int i = 0; i < car.wheel.length; i++) {//给四个轮子充气 car.wheel[i].inflate(100); } car.engine.start(); car.door[0].win.roolUp(); car.door[1].win.roolDown(); car.door[0].open(); car.door[1].close(); car.engine.stop(); } }