Java 习题 (71)

题目:
在Car.java中给Engine添加一个service()方法,并在main()中调用该方法。

解答:
先提供原先的Car.java代码:

class Engine {
    
    
    public void start() {
    
    }
    public void rev() {
    
    }
    public void stop() {
    
    }
}

class Wheel {
    
    
    public void inflate(int psi) {
    
    }
}

class Window {
    
    
    public void rollup() {
    
    }
    public void rolldown() {
    
    }
}

class Door {
    
    
    public Window window = new Window();
    public void open() {
    
    }
    public void close() {
    
    }
}

public class Car{
    
    
    public Engine engine = new Engine();
    public Wheel[] wheel = new Wheel[4];
    public Door left = new Door(), right = new Door();
    public Car() {
    
    
        for(int i = 0; i<4; i++){
    
    
            wheel[i] = new Wheel();
        }
    }
    public static void main(String[] args){
    
    
        Car car = new Car();
        car.left.window.rollup();
        car.wheel[0].inflate(72);
    }
}

根据上面的代码,来完成题目的要求:

class Engine {
    
    
    public void start() {
    
    }
    public void rev() {
    
    }
    public void stop() {
    
    }
    public void service() {
    
    
        System.out.println("This is the service function for the Engine");
    }
}

class Wheel {
    
    
    public void inflate(int psi) {
    
    }
}

class Window {
    
    
    public void rollup() {
    
    }
    public void rolldown() {
    
    }
}

class Door {
    
    
    public Window window = new Window();
    public void open() {
    
    }
    public void close() {
    
    }
}

public class Car{
    
    
    public Engine engine = new Engine();
    public Wheel[] wheel = new Wheel[4];
    public Door left = new Door(), right = new Door();
    public exerciseThree() {
    
    
        for(int i = 0; i<4; i++){
    
    
            wheel[i] = new Wheel();
        }
    }
    public static void main(String[] args){
    
    
        Car car = new Car();
        car.left.window.rollup();
        car.wheel[0].inflate(72);
        car.engine.service();
    }
}

结果如下:
在这里插入图片描述
如果觉得不错,就用点赞或者关注或者留言,来代替五星好评~
谢谢各位~

猜你喜欢

转载自blog.csdn.net/BSCHN123/article/details/107903732
71