1.程序
package Demo1;
abstract class MotoVehicle{
private String No;
private String Brand;
private String Color;
private String Mileage;
public String getNo() {
return No;
}
public void setNo(String no) {
No = no;
}
public String getBrand() {
return Brand;
}
public void setBrand(String brand) {
Brand = brand;
}
public String getColor() {
return Color;
}
public void setColor(String color) {
Color = color;
}
public String getMileage() {
return Mileage;
}
public void setMileage(String mileage) {
Mileage = mileage;
}
public MotoVehicle() {
super();
}
public MotoVehicle(String no, String brand, String color, String mileage
) {
super();
No = no;
Brand = brand;
Color = color;
Mileage = mileage;
//Cost = cost;
}
abstract void CalcRent();
}
class Car extends MotoVehicle{
private String Type;
private int Cost;
public int getCost() {
return Cost;
}
public void setCost(int cost) {
Cost = cost;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public Car() {
super();
}
public Car(String type) {
super();
Type = type;
}
public Car(String no, String brand, String color, String mileage,String type,int Cost) {
super(no, brand, color, mileage);
Type = type;
Cost=getCost();
}
void CalcRent() {
System.out.println(getNo()+","+Type);
if(getNo().equalsIgnoreCase("GL8")&&Type.equalsIgnoreCase("别克商务舱")){
Cost=600;
}else if(getNo().equalsIgnoreCase("550i")&&Type.equalsIgnoreCase("宝马")){
Cost=500;
}else{
Cost=300;
}
System.out.println(getNo()+","+getBrand()+","+getColor()+","+getMileage()+","+Cost+","+Type);
}
}
class Bus extends MotoVehicle{
private int SeatCount;
private int Cost;
public int getCost() {
return Cost;
}
public void setCost(int cost) {
Cost = cost;
}
public int getSeatCount() {
return SeatCount;
}
public void setSeatCount(int seatCount) {
SeatCount = seatCount;
}
public Bus() {
super();
}
public Bus(int seatCount) {
super();
SeatCount = seatCount;
}
public Bus(String no, String brand, String color, String mileage,
int SeatCount,int cost) {
super(no, brand, color, mileage);
SeatCount = getSeatCount();
Cost=getCost();
}
void CalcRent() {
if(getBrand().equalsIgnoreCase("客车")&&getSeatCount()<=16){
Cost=800;
}else{
Cost=1500;
}
System.out.println(getNo()+","+getBrand()+","+getColor()+","+getMileage()+","+SeatCount+","+Cost);
}
}
public class Test1 {
public static void main(String[] args) {
Car c1=new Car("GL8", "轿车", "银灰色", "30/1", "别克商务舱",600);
c1.CalcRent();
System.out.println();
Car c2=new Car("550i", "轿车", "黑色", "60/1", "宝马",500);
c2.CalcRent();
System.out.println();
Car c3=new Car("GL1", "轿车", "黄色", "60/1", "别克林荫大道",300);
c3.CalcRent();
System.out.println();
Bus b1=new Bus("1", "客车", "黄色", "30/1", 20, 1500);
b1.CalcRent();
System.out.println();
Bus b2=new Bus("2", "客车", "蓝色", "20/1", 16, 800);
b2.CalcRent();
}
}
2.截图