测试: 0.main 函数入口
package java2.chapter61;
import java.util.Scanner;
public class Test {
static boolean step1 = true;
static boolean step2 = true;
static boolean yesOrNo = false;
static int number = 0;
static Auto au = new Auto();
static CarAudi cA = new CarAudi("奥迪A4","小轿车",500,4);
static Auto cB = new CarBWM("宝马X1","小轿车",600,4);
static Truck gt = new Truck("依维柯01","货车",1000,2,10);
static Auto gp = new Pick_up("皮卡雪6","货车",450,4,1);
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是0否");
input1Method();//调用方法1
if(yesOrNo){
System.out.println("\n您可租车的类型及价目表:");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1. 奥迪A4 500元/天 载人:4人");
System.out.println("2. 宝马X1 550元/天 载人:4人");
System.out.println("3. 依维柯01 1000元/天 载货:20吨");
System.out.println("4. 皮卡雪6 450元/天 载人:4人 载货:2吨");
System.out.println("\n请输入您要租车的序号:");
Test.input2Method();//调用方法2,可以用本类名.方法 调用
System.out.println("\n请输入您要租该车多少天");
input3Method();
}
}
static public void input1Method() { //是否租车
while (step1) {
String input1 = sc.next();
switch (input1) {
case "1":
System.out.println("租车");
step1 = false;
yesOrNo = true;
break;
case "0":
System.out.println("不租车");
step1 = false;
yesOrNo = false;
break;
default:
System.out.println("输入无效,请重新输入");
step1 = true;
yesOrNo = false;
break;
}
}
}
static public void input2Method() { //选择车型
while (step2) {
String input2 = sc.next();
switch (input2) {
case "1":
cA.printPrice();
number = cA.getPrice();
step2 = false;
break;
case "2":
cB.printPrice();
number = cB.getPrice();
step2 = false;
break;
case "3":
gt.printPrice();
number = gt.getPrice();
step2 = false;
break;
case "4":
gp.printPrice();
number = gp.getPrice();
step2 = false;
break;
default:
System.out.println("输入有误,请重新输入");
step2 = true;
break;
}
}
}
static public void input3Method(){
int input3 = sc.nextInt();
number = input3*number;
System.out.println("总价钱是:"+number+"元人民币");
}
}
父类:1.汽车
package java2.chapter61;
/*
- 汽车父类
-
*/
public class Auto {
private String type = "";//型号
private String name = "汽车";//类型
private int price = 10; //价格
private int num = 0;//载客人数
private int much = 0;//多少天public String getTypt(){
return type;
}
public void setType(String type){
this.type = type;
}
public int getMuch(){
return much;
}
public void setMuch(int much){
this.much = much;
}
public int getNum(){
return num;
}
public void setNum(int num){
this.num = num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void printPrice(){
System.out.println("型号:"+type);
System.out.println("类型:"+name);
System.out.println("租金:"+price+"元/天");
System.out.println("载客:"+num+"人");
}
}
接口: 2.货车接口
package java2.chapter61;
/*
- 货车接口
- 具有载货量的汽车
-
*/
public interface Goods_van {
void goodsVan();
}
子类: 3.奥迪小轿车
package java2.chapter61;
/*
- 奥迪小轿车,载客5人座
*/
public class CarAudi extends Auto {
public CarAudi(String type,String name,int price,int num) { //创建有参构造函数
super.setType(type);
super.setName(name);
super.setPrice(price);
super.setNum(num);
}
public void printPrice(){
super.printPrice();
}
}
子类:4.宝马小轿车
package java2.chapter61;
/*
- 宝马小轿车,载客5人座
*/
public class CarBWM extends Auto {
public CarBWM(String type,String name,int price,int num) {
super.setType(type);
super.setName(name);
super.setPrice(price);
super.setNum(num);
}
public void printPrice(){
super.printPrice();
}
}
子类:5.大卡车
package java2.chapter61;
/*
- 大卡车,载货10吨
*/
public class Truck extends Auto implements Goods_van{
int weight;
public Truck(String type,String name,int price,int num,int weight) {
super.setType(type);
super.setName(name);
super.setPrice(price);
super.setNum(num);
this.weight = weight;
}
@Override
public void goodsVan() {
System.out.println("载货:"+weight+"吨");
}
public void printPrice(){
super.printPrice();
this.goodsVan();
}
}
子类:6.皮卡车
package java2.chapter61;
/*
- 皮卡车,具有载客和载货功能
- 载客4人,载货1吨
*/
public class Pick_up extends Auto implements Goods_van{
int weight;
public Pick_up(String type,String name,int price,int num,int weight) {
super.setType(type);
super.setName(name);
super.setPrice(price);
super.setNum(num);
this.weight = weight;
}
@Override
public void goodsVan() {
System.out.println("载货:"+weight+"吨");
}
public void printPrice(){
super.printPrice();
this.goodsVan();
}
}