//父类
package com.dada;
public class Vehicle
{
private int number, rent, capacityMan, capacityCargo;
private String brandName;
public Vehicle(int number, String brandName, int rent, int capacityMan, int capacityCargo)
{
this.number=number;
this.brandName=brandName;
this.rent=rent;
this.capacityMan=capacityMan;
this.capacityCargo=capacityCargo;
}
public int getNumber()
{
return number;
}
public int getRent()
{
return rent;
}
public int getCapacityMan()
{
return capacityMan;
}public int getCapacityCargo()
{
return capacityCargo;
}
public String getBrandName()
{
return brandName;
}
}
//子类1
package com.dada;
public class ForMan extends Vehicle
{
public ForMan(int number, String brandName, int rent, int capacityMan, int capacityCargo)
{
super(number, brandName, rent, capacityMan, capacityCargo);
// TODO Auto-generated constructor stub
System.out.println(number+"\t"+brandName+"\t"+rent+"\tMan:"+capacityMan);
}
}
//子类2
package com.dada;
public class ForCargo extends Vehicle
{
public ForCargo(int number, String brandName, int rent, int capacityMan, int capacityCargo)
{
super(number, brandName, rent, capacityMan, capacityCargo);
// TODO Auto-generated constructor stub
System.out.println(number+"\t"+brandName+"\t"+rent+"\tCargo:"+capacityCargo);
}
}
//子类3
package com.dada;
public class ForBoth extends Vehicle
{
public ForBoth(int number, String brandName, int rent, int capacityMan, int capacityCargo)
{
super(number, brandName, rent, capacityMan, capacityCargo);
System.out.println(number+"\t"+brandName+"\t"+rent+"\tMan:"+capacityMan+"\tCargo:"+capacityCargo);
}
}
//main中代码实现
package com.dada;
import java.util.*;
public class DadaTest
{
public static void main(String[] args)
{
System.out.println("Welcome to DADA System! Would you like rent a car today? Choose 1 for Yes and 0 for exit.");
Scanner scan=new Scanner(System.in);
int input=scan.nextInt();
if(input==1)
{
System.out.println("***The List of Cars and Rent as below***");
System.out.println("No.\tBrand\tRent\tCapacity");
Vehicle []All={new ForMan(1, "AudiA4", 500, 4,0),
new ForMan(2, "Mazda6", 400, 4,0),
new ForBoth(3,"Pika6",450,4,2),
new ForMan(4,"Jinlong",800,20,0),
new ForCargo(5,"Shj", 400,0,4),
new ForCargo(6,"Ivike", 1000, 0, 20)};
System.out.println("Please input the number of your cars");
int inputNumber=scan.nextInt();
int load=0;
int loads=0;
int price=0;
int prices=0;
String brands=" ";
String brandss=" ";
for(int i=1;i<=inputNumber;i++)
{
System.out.print("Please input the "+i+" car's No.:");
int number=scan.nextInt();
//price=price+(All[number-1].getRent());
//brands=brands+(All[number-1].getBrandName());
//load=load+(All[number-1].getCapacityMan()+All[number-1].getCapacityCargo());
if(number==1number==2number==3number==4)
{
brands=brands+(All[number-1].getBrandName());
load=load+(All[number-1].getCapacityMan());
price=price+(All[number-1].getRent());
}
if(number==3number==5number==6)
{
brandss=brandss+(All[number-1].getBrandName());
loads=loads+(All[number-1].getCapacityCargo());
prices=prices+(All[number-1].getRent());
}
if(number==3)
{
prices=0;
}
}
System.out.println("Please days: ");
int day=scan.nextInt();
System.out.println("Your bill: ");
System.out.println("***Forman: "+brands+"\tTotal\t"+load+" people");
System.out.println("***Forcargo: "+brandss+"\tTotal\t"+loads+" cargo");
System.out.println("The total price is:\t"+(prices+price)*day);
}
if(input==0)
{
System.out.println("See you next time!");
}
}
}
热门评论
//父类
package com.dada;
public class Vehicle {
private int number, rent, capacityMan, capacityCargo;
private String brandName;
请问这里的参数定义为什么要用private私有修饰符修饰,而不用public?
//父类
package com.dada;
public class Vehicle {
private int number, rent, capacityMan, capacityCargo;
private String brandName;
请问这里的参数定义为什么要用private私有修饰符修饰,而不用public?