为什么我写到main的时候,在创建数值时一直报The constructor PassengerCar(int, String, String, String) is undefined这个错,哪个大神帮你解答一下
package com.imooc;
父类
public class car {
private int number;//车辆编号
private String name;//车辆名称
private float price;//车辆租金
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "序号:" +number+"\t"+"名称:"+name+"\t"+"价格:"+price+"\t";
}
}
子类 载客量
package com.imooc;
public class PassengerCar extends car {
private int passenger;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
super.setNumber(newNumber);
super.setName(newName);
super.setPrice(newPrice);
this.setPassenger(passenger);
}
@Override
public String toString() {
return super.toString()+"载客量:"+passenger;
}
}
子类 载货量
package com.imooc;
public class TruckCar extends car {
private double capacity;
public double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public TruckCar(int newNumber,String newName,float newPrice,double capacity) {
super.setName(newName);
super.setNumber(newNumber);
super.setPrice(newPrice);
this.setCapacity(capacity);
}
@Override
public String toString() {
return super.toString()+"载货量:"+capacity;
}
}
子类 皮卡
package com.imooc;
public class PickCar extends car {
private int passenger;
private double capacity;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public PickCar(int newNumber,String newName,float newPrice,int passenger,double capacity) {
super.setName(newName);
super.setNumber(newNumber);
super.setPrice(newPrice);
this.setPassenger(passenger);
this.setCapacity(capacity);
}
@Override
public String toString() {
return super.toString()+"载客量:"+passenger+"\t"+"载货量:"+capacity;
}
}
main
package com.imooc;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建汽车对象
car[] cars = new car[5];
cars[0]= new PassengerCar(1,"奥迪A4","500f","4");
cars[1]=new TruckCar(2,"松花江","400f","4")
}
}
构造器的参数类型是(int,String,float,int),但是你创建的时候确是(int,String,String,String)
public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
super.setNumber(newNumber);
super.setName(newName);
super.setPrice(newPrice);
this.setPassenger(passenger);
}
这里设计的不是很好
首先super.setNumber(newNumber);这个只是初始化了父类的Number属性,但又因为你设计的父类的属性是私有的,私有属性是不可以被继承的。所以这个初始化,你的子类只有一个属性被初始化了,就是passenger,没有其他属性了。
还有,创建对象是car类首字母要大写
建议:
首先将父类公有化
然后
建议是:
首先将父类属性公有化,这样子类才继承有父类那3个属性
其次private在同一类中是可以访问的,这里完全可以这样子写
public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
//初始化子类属性
this.setNumber=newNumber;
this.setName=newName;
this.setPrice=newPrice;
this.setPassenger(passenger);
}
希望楼主无论解决与否给个回复,谢谢~
创建汽车对象的时候,对象不应该用数组。
car[] cars = new car[5]; 实际上创建的实际上是一个car类型的数组
换成 car cars = new car();
cars.PassengerCar(1,"奥迪A4","500f","4");
cars.TruckCar(2,"松花江","400f","4");
楼主这样试一下,应该可以