多类合作
秋名山代码实操题目
主类
package com.chuxing.a;
public class dome1 {
/*
* 这里会出现异常,Thread.sleep(500);
* 这个代码是在car类内,但是调用了就会出现异常,所以需要在使用的地方处理异常。
*/
public static void main(String []args) throws InterruptedException{
Car car=new Car("奔驰","黑色",4);
Factory factory=new Factory("秋名山汽修店","秋名山山脚下豆腐店旁边","133*******");
while(car.getSpeed()<=600){
car.race();
if(car.getSpeed()>300){
/*
* 这里设置的速度超过阈值就会爆胎
* 需要去修理
* 这里设置的是无限循环的代码。
*/
car.setWheelcount(3);
car.race();
factory.repair(car);
}
}
}
}
汽车类
package com.chuxing.a;
public class Car {
private String name ;
private String color ;
private int wheelcount ;
private int speed = 0 ;
public Car(){}//无参构造方法
public Car(String name ,String color,int wheelcount){//有参构造方法
this.color=color;
this.name=name;
this.wheelcount=wheelcount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheelcount() {
return wheelcount;
}
public void setWheelcount(int wheelcount) {
this.wheelcount = wheelcount;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void race() throws InterruptedException{
if(4==wheelcount){
System.out.println("开着"+color+"的"+name+"在秋名山 速度为"+speed);
speed=speed+50;
/*
* 这里就是异常Thread.sleep(500);
* 需要ctrl+1选择第一项
* 我这里设置了一个变量速度speed
* 主类内检测速度值超过阈值之后就会爆胎
*/
Thread.sleep(500);
}
else {
System.out.println("速度过快车胎炸裂,赶快去修理");
}
}
}
修理厂类
package com.chuxing.a;
public class Factory {
private String name;
private String address;
private String number;
public Factory(){}
public Factory(String name,String address,String number){
this.address=address;
this.name=name;
this.number=number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public void repair(Car car) throws InterruptedException{
System.out.println(name);
System.out.println(address);
System.out.println(number);
if(car.getWheelcount()<4){
System.out.println("车辆一个轮子爆胎了,现在给你修理好啦!注意安全上路把!");
car.setWheelcount(4);
car.setSpeed(0);
}
else
System.out.println("车辆没事赶快走把");
Thread.sleep(1000);
}
}