为了账号安全,请及时绑定邮箱和手机立即绑定

怎么把这种固定车辆变成可添加的呢? 是直接在数组上面加? 还是就是我觉得我直接这样写的代码不太严谨,怎么修改呢?

package com.dadazhuche;
import com.car.*;
import java.util.*;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] che= {new Passeng("奥迪A4",500,4),new Passeng("马自达6",400,4),new Passeng("金龙",800,20)
                ,new Pika("皮卡雪",450,4,2),new Carry("松花江",400,4),new Carry("依维柯",1000,20)};
        System.out.println("是否要租车:1:是,0:否");
        Scanner o=new Scanner(System.in);
        if(o.nextInt()==1) {
            System.out.println("序号\t汽车名称\t金额\t\t容量");
            for(int i=0;i<che.length;i++) {
                System.out.println((i+1)+che[i].toString());
            }
        }
        else {
            System.out.println("欢迎再来");
        }
        System.out.println("请输入要租车的数量");
        Scanner p=new Scanner(System.in);
        int e=p.nextInt();
        if(e>0) {
            System.out.println("             ");
            System.out.println("输入要租的天数");
            Scanner s=new Scanner(System.in);
            int j =s.nextInt();
            int a=0;
            if(j>0) {
                for(int i=1;i<=e;i++) {
                    System.out.println("输入序号"+i+"车");
                    Scanner w=new Scanner(System.in);
                    int k=w.nextInt();
                    a+=che[k-1].cost(j);
                }
                System.out.println("价格为"+a+"元");
            }
        }
    }
}




父类car

package com.car;

public class Car {

    public int cost(int j){
        return 0;
    }
}


三大子类

package com.car;

public class Carry extends Car {
    String name;
    int price;
    int weigh;
    public Carry(String name,int price,int weigh) {
        this.name=name;
        this.price=price;
        this.weigh=weigh;
    }
    public int cost(int j){
        int sum=this.price*j;
        return sum;
    }
    @Override
    public String toString() {
        return "\t"+name + "\t" + price+"元/天\t"+ "\t载货:"+weigh+"吨  ";
    }
    
}



package com.car;

public class Passeng extends Car {
    String name;
    int price;
    int capacity;
    public Passeng(String name,int price,int capacity) {
        this.name=name;
        this.price=price;
        this.capacity=capacity;
    }
    public int cost(int j){
        int sum=this.price*j;
        return sum;
    }
    public String toString() {
        return "\t"+name + "\t" + price +"元/天\t"+ "\t载人:"+capacity+"人 ";
    }
}



package com.car;

public class Pika extends Car {
    String name;
    int price;
    int weigh;
    int capacity;
    public Pika(String name,int price,int capacity,int weigh) {
        this.name=name;
        this.price=price;
        this.weigh=weigh;
        this.capacity=capacity;
    }
    public int cost(int j){
        int sum=this.price*j;
        return sum;
    }

    public String toString() {
        return "\t"+name + "\t" + price+"元/天\t" + "\t载人:"+capacity+"人 载货: "+weigh+"吨";
    }
    

}







正在回答

1 回答

public class EasycarTest {


public static void main(String[] args) {

// TODO Auto-generated method stub

Car[] car=new Car[10];

car[0]=new PassengerCar("奥迪A4",500,4);

car[1]=new PassengerCar("马自达6",400,4);

car[2]=new PickupCar("皮卡雪6",450,2,4);

car[3]=new PassengerCar("金龙          ",800,20);

car[4]=new Truck("松花江",400,4);

car[5]=new Truck("依维柯",1000,20);

/*

如果要从界面输入就需要做界面录入的代码了.仅供参考



0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

怎么把这种固定车辆变成可添加的呢? 是直接在数组上面加? 还是就是我觉得我直接这样写的代码不太严谨,怎么修改呢?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信