❶ 用java创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造方法
public class Car {
private int color; // 颜色 0:未定义 1:红 2:黄 3:蓝 ...
private int type ; // 型号 0:未定义 1:轿车 2:卡车 3:大巴 4:越野车
// 无形参的构造方法
public Car(){
this.type = 1;
this.color = 1;
}
// 有形参的构造方法
public Car(int type, int color ){
this.type = type;
this.color = color;
}
// 显示颜色
public void PrintColor(){
String strColor = "";
switch(this.color){
default:
case 0:
strColor = "未定义颜色";
break;
case 1:
strColor = "红";
break;
case 2:
strColor = "黄";
break;
case 3:
strColor = "蓝";
break;
}
System.out.print(strColor);
}
// 显示型号
public void PrintType(){
String strType = "";
switch(this.type){
default:
case 0:
strType = "未定义型号";
break;
case 1:
strType = "轿车";
break;
case 2:
strType = "卡车";
break;
case 3:
strType = "大巴";
break;
case 4:
strType = "越野车";
break;
}
System.out.print(strType);
}
}