public class TestVehicle{
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.setBrand("宝马");
v.setColor("黑色");
System.out.println(v.getBrand()+"\t"+v.getColor());
v.move();
v.showInfo("宝马","白色");
}
}
class Vehicle{
private String brand;
private String color;
public void setBrand(String brand){
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
public Vehicle(){}
public Vehicle(String brand,String color){
this.brand = brand;
this.color = color;
}
public void move(){
System.out.println("我已经启动");
}
public void showInfo(String brand,String color){
System.out.println("商标是"+brand+"\t"+"颜色是"+color);
}
}
(封装)编程:定义一个交通工具类(Vehicle),类中属性有商标(brand)、颜色(color)
于 2022-10-08 16:35:43 首次发布