定义车的类型(大驼峰)
要求:
+构造方法
+属性(小驼峰)
+行为
测试:
+主方法
+构造汽车数组,输出汽车信息
下面来完成这个类
首先,定义一个类
public class Car{
然后,完成这个车的信息
//属性
//发动机编号
private String engineNumber;
//品牌
private String brand;
//颜色
private String color;
//ture 自动挡 false 手动挡
private boolean autoType;
//构造方法
//方法的参数名:符合标识符命名规范,简明思议
public Car(String engineNumber){
this.engineNumber=engineNumber;
}
//getter setter
//this 该类实例化的对象及当前操作的对象
public String getEngineNumber(){
return this.engineNumber;
}
//brand品牌
public String getBrand(){
return this.brand;
}
public void setBrand(String brand){
this.brand=brand;
}
//color颜色
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
//属性是布尔型的getter方法 isXxx
public boolean isAutoType(){
return this.autoType;
}
public void setAutoType(boolean autoType){
this.autoType=autoType;
}
//行为(功能)
public void drive(){
System.out.println("喝酒不开车,开车不喝酒");
//结合方法使用
if(this.isAutoType()){
System.out.println("自动挡,好开");
}else{
System.out.println("手动挡,不好开");
}
}
public void back(){
System.out.println("倒车,请注意");
}
public String getCarDescription(){
return "发动机编号:"+this.getEngineNumber()+"\n"
+"品牌:"+this.brand+"\n"
+"颜色:"+this.color+"\n"
+"是否自动挡:"+this.isAutoType();
}
主方法中构造并输出
public static void main(String[] args){
Car wlhg=new Car("wlhgl008757");
wlhg.setAutoType(true);
wlhg.drive();
wlhg.setColor("白色");
wlhg.setBrand("五菱宏光");
String desc=wlhg.getCarDescription();
System.out.println(desc);
}