• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

这个照着打的怎么输出的没有数组中的值啊?

package com.xml.rentcar;//父类public class Car {private int num;//数量private String name;//车名private int rent;//租金private int manned;//载货private int burden;//容量public Car(int num, String name, int rent, int manned, int burden) {                this.num = num;        this.name = name;        this.rent = rent;        this.manned = manned;        this.burden = burden;}public Car(){        }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 getRent() {        return rent;}public void setRent(int rent) {        this.rent = rent;}public int getManned() {        return manned;}public void setManned(int manned) {        this.manned = manned;}public int getBurden() {        return burden;}public void setBurden(int burden) {        this.burden = burden;}}-----------------------

package com.xml.rentcar;


/*
* 载人的子类
*/
public class MannedCar extends Car {
       
        private int manned;


        public int getManned() {
                return manned;
        }


        public void setManned(int manned) {
                this.manned = manned;
        }


        public MannedCar(int manned,String name,int rent,int num) {
               
                this.manned = manned;
        }
       
}
-----------------------
package com.xml.rentcar;


/*
* 载货的子类
*/
public class BurdenCar extends Car {
        private int burden;


        public int getBurden() {
                return burden;
        }


        public void setBurden(int burden) {
                this.burden = burden;
        }


        public BurdenCar(int num,String name,int rent,int burden) {
               
                this.burden = burden;
        }
       
}
-------------
package com.xml.rentcar;


/*
* 既有载人也有载货的子类
*/
public class BothCar extends Car {
        private int manned;
        private int burden;
        public int getManned() {
                return manned;
        }
        public void setManned(int manned) {
                this.manned = manned;
        }
        public int getBurden() {
                return burden;
        }
        public void setBurden(int burden) {
                this.burden = burden;
        }
        public BothCar(int num,String name,int rent,int manned, int burden) {
               
                this.manned = manned;
                this.burden = burden;
        }
       
}
----------------
package com.xml.rentcar;


import java.util.Scanner;




public class Main {
        public static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
                System.out.println("欢迎来到涛涛租车系统:");
                System.out.println("你是否要租车呀?1是,0否");
                sc = new Scanner(System.in);
                int a = sc.nextInt();
                if(a==1){
                        System.out.println("你可以租车的类型及价目表");
                        System.out.println("序号 汽车名称  租金 容量");
                        Car[] types = { new MannedCar(1, "奥迪A4", 500, 4),
                                        new MannedCar(2, "马自达", 400, 4),
                                        new MannedCar(3, "上海大众", 600, 3),
                                        new MannedCar(4, "吉利自由舰", 200, 5),
                                        new MannedCar(5, "凯迪拉克", 600, 4) };
                        for (Car car : types) {
                                if(car instanceof MannedCar){
                                        System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载人"+car.getBurden());
                                }
                                else if(car instanceof BurdenCar){
                                        System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载货"+car.getBurden());
                                }
                                else {
                                        System.out.println(car.getNum()+"\t"+car.getName()+"\t"+car.getRent()+"\t"+"载人:"+car.getManned()+"\t"+"载货"+car.getBurden());
                                }
                        }
                        System.out.println("请输入你要租车的数量:");
                        sc = new Scanner(System.in);
                        int num = sc.nextInt();

                        while(num

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与2

你在初始化car的子类里面没有显示调用父类的方法,所以它会默认调用父类不带参数的构造方法,这样的话就不会对父类里面的变量进行初始化了,如果你想初始化父类里面的变量应该在子类里面显示调用父类的带参数的构造方法。

使用道具 举报

已解决是的

使用道具 举报

发新帖

发布任务需求已有1031165位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!