/**
* 电梯类
* @author H2
*
*/
public class Elevator {
public int currentFloor=1;//当前楼层
public int destinationFloor;//将要前往的楼层
public boolean up;//上行或下降
public boolean doorsOpen;//电梯关门或开门
public Elevator(int currentFloor){
this.currentFloor=currentFloor;
System.out.println("这是一个有参构造方法");
}
public Elevator(){
this(1);
System.out.println("这是一个无参构造方法");
}
//开门
public void openDoors(){
doorsOpen=true;
System.out.println("电梯正在开门......");
}
//关门
public void closeDoors(){
doorsOpen=false;
System.out.println("电梯正在关门.......");
}
//电梯上行
public void goingUp(){
up=true;
System.out.println("电梯正在上行.......");
}
//电梯下降
public void goingDown(){
up=false;
System.out.println("电梯正在下降.......");
}
public void goToFloor(int floor){
System.out.println("将要去"+floor+"层");
if(floor>currentFloor){
//上升
goingUp();
currentFloor=floor;
System.out.println("电梯当前位置:"+currentFloor);
}else if(floor<currentFloor){
//下降
goingDown();
currentFloor=floor;
System.out.println("电梯当前位置:"+currentFloor);
}else if(floor==currentFloor){
System.out.println("当前楼层就是目标楼层");
}
}
}
public static void main(String[] args) {
System.out.println("===============================");
Elevator elevator1=new Elevator();
elevator1.goToFloor(100);
Elevator elevator2=new Elevator(73);
elevator2.goToFloor(1);
/*elevator1.goToFloor(40);
elevator1.goToFloor(10);
elevator1.goToFloor(14);
elevator1.goToFloor(50);
elevator1.goToFloor(100);
elevator1.goToFloor(4);
elevator1.goToFloor(1);
elevator1.goToFloor(20);
elevator1.goToFloor(14);*/
System.out.println("===============================");
}