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

正在回答

5 回答

请问父类构建器为啥要用super呢


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

第一段代码中,

super()和this的作用分别是什么呢


0 回复 有任何疑惑可以回复我~
<?php
echo "真棒!";


0 回复 有任何疑惑可以回复我~
package com.imooc.newdada;

public class Car {
	public int carId;
	public String carName;
	public double carRental;
	public Car(int carId, String carName, double carRental) {
		super();
		this.carId = carId;
		this.carName = carName;
		this.carRental = carRental;
	}
}
package com.imooc.newdada;

public class PassengerCar extends Car{
	int carCapacity;
	
	public PassengerCar(int carId, String carName, double carRental,
			int carCapacity) {
		super(carId, carName, carRental);
		this.carCapacity = carCapacity;
	}	

}
package com.imooc.newdada;

public class Truck extends Car {
	int  capForgoods;
	
	public Truck(int carId, String carName, double carRental, int capForgoods) {
		super(carId, carName, carRental);
		this.capForgoods = capForgoods;
	}
}
package com.imooc.newdada;

public class PickupTruck extends Car {
	int carCapacity;
	int  capForgoods;
	public PickupTruck(int carId, String carName, double carRental,
			int carCapacity, int capForgoods) {
		super(carId, carName, carRental);
		this.carCapacity = carCapacity;
		this.capForgoods = capForgoods;
	}

}
package com.imooc.newdada;

public class DadaIndex {
	public static void main(String[] args) {
	System.out.println("ID\t"+"车类型\t"+"租费/天\t"+"可载人数\t"+"可载吨数");
	PassengerCar[] passengerCar={new PassengerCar(1, "奔驰A2", 500, 4)};
	PickupTruck[] pickupTruck={new PickupTruck(2,"皮卡",400,2,200)};	
	Truck[] truck={new Truck(3,"大众货车",800,1000)};
	showCars(passengerCar, pickupTruck, truck);
	}
	public static void showCars(PassengerCar[] passengerCar,PickupTruck[] pickupTruck,	Truck[] truck){
		for(int i=0;i<passengerCar.length;i++){
				System.out.println(passengerCar[i].carId+"\t"+passengerCar[i].carName+"\t"+passengerCar[i].carRental+"\t"+passengerCar[i].carCapacity+"\t"+null);
		}
		for(int i=0;i<pickupTruck.length;i++){
				System.out.println(pickupTruck[i].carId+"\t"+pickupTruck[i].carName+"\t"+pickupTruck[i].carRental+"\t"+pickupTruck[i].carCapacity+"\t"+pickupTruck[i].capForgoods);
		}
		for(int i=0;i<truck.length;i++){
				System.out.println(truck[i].carId+"\t"+truck[i].carName+"\t"+truck[i].carRental+"\t"+null+"\t"+truck[i].capForgoods);
		}
	}
}

http://img1.sycdn.imooc.com//5aad50510001991a03620094.jpg

3 回复 有任何疑惑可以回复我~
package com.imooc.dada;
//父类
public class Car {
	public int carId;
	public String carName;
	public double carRental;
	public int carCapacity;
	public int capForgoods;
	public Car(int carId, String carName, double carRental, int carCapacity) {
		super();
		this.carId = carId;
		this.carName = carName;
		this.carRental = carRental;
		this.carCapacity = carCapacity;
	}
	
	public Car(int carId, String carName, double carRental, int carCapacity,
			int capForgoods) {
		super();
		this.carId = carId;
		this.carName = carName;
		this.carRental = carRental;
		this.carCapacity = carCapacity;
		this.capForgoods = capForgoods;
	}
}
package com.imooc.dada;

public class PassengerCar extends Car {

	public PassengerCar(int carId, String carName, double carRental,
			int carCapacity) {
		super(carId, carName, carRental, carCapacity);
		// TODO Auto-generated constructor stub
	}

}
package com.imooc.dada;

public class PickupTruck extends Car{

	public PickupTruck(int carId, String carName, double carRental,
			int carCapacity, int capForgoods) {
		super(carId, carName, carRental, carCapacity, capForgoods);
		// TODO Auto-generated constructor stub
	}
	

}
package com.imooc.dada;

public class Truck extends Car {

	public Truck(int carId, String carName, double carRental, int carCapacity) {
		super(carId, carName, carRental, carCapacity);
		// TODO Auto-generated constructor stub
	}

}
package com.imooc.dada;

import java.util.Scanner;

public class DadaIndex {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Car[] cars={new PassengerCar(1,"奥迪A4",500,4),new PassengerCar(2,"马自达6",400,4),new PickupTruck(3, "皮卡雪5", 500, 2, 253),new PickupTruck(4, "皮卡雪6", 1060, 2, 520),new PassengerCar(5,"奔驰A6",600,4),new Truck(6,"松花江",400,400)};
		System.out.println("欢迎使用答答租车系统"+"\n"+"您是否要租车:1是   0否");	
		isNeed();
		System.out.println("您可租车的类型及其价目表:");
		carList(cars);
		System.out.println("请输入您要租车的数量:");
		int num=getNum();
		int[] arr=chooseCar(num);
		System.out.println("请输入租车天数:");
		int days=getDays();
		System.out.println("您所选的车辆信息及费用如下:");
		System.out.println("ID\t"+"车类型\t"+"租费/天\t"+"可载人数\t"+"可载吨数");
		getCarInfo(arr,cars,days);
	}
	public static void isNeed(){
		Scanner input=new Scanner(System.in);
		int choose=input.nextInt();
		if(choose==1){
			return;
		}else{
			System.exit(0);
		}
	}
	public static void carList(Car[] cars){	
		System.out.println("ID\t"+"车类型\t"+"租费/天\t"+"可载人数\t"+"可载吨数");
		for(int i=0;i<cars.length;i++){
			System.out.println(cars[i].carId+"\t"+cars[i].carName+"\t"+cars[i].carRental+"\t"+cars[i].carCapacity+"\t"+cars[i].capForgoods);
				}	
	}
	public static int getNum(){
		Scanner input1=new Scanner(System.in);
		int num=input1.nextInt();
		return num;
	}
	public static int[] chooseCar(int num){
		int[] arr=new int[num];
		int i;
		double sum;
		for(i=0;i<num;i++){
			System.out.println("请选择第"+(i+1)+"辆车的id:");
			Scanner input2=new Scanner(System.in);
			 arr[i]=input2.nextInt();
		}	
		return arr;
	}
	public static int getDays(){
		Scanner input1=new Scanner(System.in);
		int days=input1.nextInt();
		return days;
	}
	public static void getCarInfo(int arr[],Car[] cars,int days){
		//用较短的数组的长度作为循环条件
		double money=0;
		int peopleNum=0;
		double weight=0;
		int i,j;
		for( i=0;i<arr.length;i++){
			for( j=0;j<cars.length;j++){
				if(arr[i]==cars[j].carId){
				money=money+cars[j].carRental*days;
				peopleNum=peopleNum+cars[j].carCapacity;
				weight=weight+cars[j].capForgoods;
				System.out.println(cars[j].carId+"\t"+cars[j].carName+"\t"+cars[j].carRental+"\t"+cars[j].carCapacity+"\t"+cars[j].capForgoods);
				}
			}
		}
		System.out.println("总费用:"+money+"\t"+"可载总人数:"+peopleNum+"\t"+"可载货物:"+weight);
		
	}	
}

http://img1.sycdn.imooc.com//5aad142e0001a63904320462.jpg

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

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530553    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

有人有最后一题的代码吗

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