程序出了点小问题,求大神帮忙看看,子类carPerson,carWeight 不能被调用。

package com;

public class Car {
	private int carNum;
	private String carName;
	private float price;
	public int getCarNum() {
		return carNum;
	}
	public void setCarNum(int carNum) {
		this.carNum = carNum;
	}
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
}

package com;

public class KeCar extends Car {
	public int carPerson;
	public KeCar(int carNum,String carName,float price,int carPerson){
		this.setCarNum(carNum);
		this.setCarName(carName);
		this.setPrice(price);
		this.setCarPerson(carPerson);
		
	}
	public int getCarPerson() {
		return carPerson;
	}

	public void setCarPerson(int carPerson) {
		this.carPerson = carPerson;
	}
}

package com;

public class PiCar extends Car {
	private float carWeight;
	private int carPerson;
	

	public int getCarPerson() {
		return carPerson;
	}

	public void setCarPerson(int carPerson) {
		this.carPerson = carPerson;
	}

	public PiCar(int carNum,String carName,float price,float carWeight,int carPerson){
		this.setCarNum(carNum);
		this.setCarName(carName);
		this.setPrice(price);
		this.carWeight=carWeight;
		this.carPerson=carPerson;
	}
	
	public float getCarWeight() {
		return carWeight;
	}
	public void setCarWeight(float carWeight) {
		this.carWeight = carWeight;
	}
}

package com;

public class KaCar extends Car {
	private float carWeight;
	
	public KaCar(int carNum,String carName,float price,float carWeight ){
		this.setCarNum(carNum);
		this.setCarName(carName);
		this.setPrice(price);
		this.carWeight=carWeight;
	}

	public float getCarWeight() {
		return carWeight;
	}

	public void setCarWeight(float carWeight) {
		this.carWeight = carWeight;
	}
	
}

package com;


import java.util.Scanner;


public class Test {


/**

* @param args

*/

public static void main(String[] args) {

float totalPrice=0f;

int totalPerson=0;

float totalWeight=0f;

System.out.println("欢迎使用租车系统");

System.out.println("您是否要租车?:1是   0否");

Scanner input=new Scanner(System.in);

int k=input.nextInt();

if(k==1){

System.out.println("租车详细信息");

Car[] carForRent={new KeCar(1,"金龙客车",400f,50),

new KeCar(2,"奥迪A4",300f,5),new PiCar(3,"雪铁龙",400f,2.5f,5),

new PiCar(4,"东风",350f,3.5f,5),new KaCar(5,"一气",700f,15.6f),

new KaCar(6,"二气",680f,15.9f)};

System.out.println("车序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量"+"\t");

for (Car Car:carForRent){

if(Car instanceof KeCar){

System.out.println(Car.getCarNum()+"\t"+Car.getCarName()

+"\t"+Car.getPrice()+"/天"+"\t"+

"载人:"+((KeCar)Car).getCarPerson()+"人");

}

if(Car instanceof PiCar){

System.out.println(Car.getCarNum()+"\t"+Car.getCarName()

+"\t"+Car.getPrice()+"/天"+"\t"+"载重"+((PiCar)Car).getCarWeight()+"吨"

+"\t"+((PiCar)Car).getCarPerson()+"人");

}

if(Car instanceof KaCar){

System.out.println(Car.getCarNum()+"\t"+Car.getCarName()

+"\t"+Car.getPrice()+"/天"+"\t"+"载重"+((KaCar)Car).getCarWeight()+"吨");

}

}

System.out.println("请输入你要租车的数量");

int nums= input.nextInt();

for(int i=0;i<nums;i++){

System.out.println("请输入你要租车的序号:");

int x =input.nextInt();

if(x>0&&x<=carForRent.length ){

if(carForRent[x-1] instanceof KeCar){

totalPerson+=nums*carForRent[x-1].carPerson;

totalWeight+=nums*carForRent[x-1].carWeight;

totalPrice+=nums*carForRent[x-1].getPrice();

}

if(carForRent[x-1] instanceof PiCar){

totalPerson+=nums*carForRent[x-1].carPerson;

totalWeight+=nums*carForRent[x-1].carWeight;

totalPrice+=nums*carForRent[x-1].getPrice();

}

if(carForRent[x-1] instanceof KaCar){

totalPerson+=nums*carForRent[x-1].carPerson;

totalWeight+=nums*carForRent[x-1].carWeight;

totalPrice+=nums*carForRent[x-1].getPrice();

}

}else{

System.out.println("输入序列号有误");

}

System.out.println("请输入您要租用的天数:");

int days= input.nextInt();

totalPerson*=days;

totalWeight*=days;

totalPrice*=days;

System.out.println("租车明细:");

System.out.println("你租的车为:"+carForRent[x-1].getCarName());

System.out.println("您租的车可以载人:"+totalPerson+"人"+"\t"+"载重:"+totalWeight+"吨"+"\t"+"费用为:"+totalPrice);

break;

}

}else if(k==0){

System.out.println("谢谢使用");

}else{

System.out.println("输入有误");

}

}


}


crazy丶BGM
浏览 1471回答 1
1回答

Linda_Je

你定义它为私有的了,就好比难道你老婆还让是public型?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java