package Shiyan2;
public class Test {
public static void main(String[] args) {
Car car = new Car("兰博基尼",2,"黄色",200,5);
car.outPut();
car.launch();
car.offPassenger();
car.onPassenger();
car.addFuel(10);
}
}
class Car {
private String brand;
private int maxPeople;
private String color;
private int maxSpeed;
private int fuel;
//有参构造
public Car(String brand,int maxPeople,String color,int maxSpeed,int fuel) {
setBrand(brand);
setMaxPeople(maxPeople);
setColor(color);
setMaxSpeed(maxSpeed);
setFuel(fuel);
}
//无参构造
public Car() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getMaxPeople() {
return maxPeople;
}
public void setMaxPeople(int maxPeople) {
this.maxPeople = maxPeople;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public int getFuel() {
return fuel;
}
public void setFuel(int fuel) {
this.fuel = fuel;
}
public void outPut() {
System.out.println("品牌:" + getBrand() + " 最大承载人数:" + getMaxPeople() + " 颜色:" + getColor() + " 最高速度:" + getMaxSpeed() + "km/h 剩余燃料:" + getFuel() + "升");
}
public void launch()
{
System.out.println("汽车发动");
}
public void onPassenger()
{
System.out.println("顾客上车");
}
public void offPassenger()
{
System.out.println("顾客下车");
}
public void addFuel(int fuel)
{
setFuel(fuel + getFuel());
System.out.println("已成功添加:" + fuel + "升油");
}
}
定义一个汽车类,要求该类中至少包含5个私有的属性(为每个属性定义设置和获取方法),该类中至少包含两个构造方法(一个无参,一个有参),以及包含5-6个方法来描述汽车对象的行为(其中一个方法要能打印输出汽
最新推荐文章于 2024-06-08 09:33:02 发布