aus*_*der 6 java constructor subclass
我已经在任务中遇到了障碍,并且已经在网站上寻找任何有用的东西(空洞的).我需要创建一个类,该类中的构造函数,然后是一个子类来扩展超类.然后,我需要使用main方法创建一个新文件来演示这两种情况.没问题,从概念上讲.
我的问题是:如何使用构造函数初始化对象,但是使用用户输入?
现在我得到的错误是:"CarRental类中的构造函数CarRental不能应用于给定类型;必需:String,int,String,int found:无参数原因:实际和形式参数列表的长度不同"
请不要嘲笑"错误告诉你问题是什么." 不,它没有告诉我它是什么.我是个小宝贝,需要一点手握.
我将在下面粘贴我的3个课程.他们可能会让你痛苦地翻腾,因为我是一个新手(同样,我的班级是一个缩短的8周课程,几乎没有时间专门用于伪代码,所以我有额外的挑战,构思逻辑本身).
我不是在寻找任何人为我做功课,我只是想在UseCarRental.java文件中寻求帮助.这是我的代码..
public class CarRental {
protected String renterName;
protected int zipCode;
protected String carSize;
protected double dailyRate;
protected int rentalDays;
protected double totalCost;
final double ECONOMY = 29.99;
final double MIDSIZE = 38.99;
final double FULLSIZE = 43.50;
public CarRental(String renterName, int zipCode, String carSize, int rentalDays){
totalCost = dailyRate * rentalDays;
}
public String getRenterName(){
return renterName;
}
public void setRenterName(String renter){
renterName = renter;
}
public int getZipCode(){
return zipCode;
}
public void setZipCode(int zip){
zipCode = zip;
}
public String getCarSize(){
return carSize;
}
public void setCarSize(String size){
carSize = size;
}
public double getDailyRate(){
return dailyRate;
}
public void setDailyRate(double rate){
switch (getCarSize()) {
case "e":
rate = ECONOMY;
break;
case "m":
rate = MIDSIZE;
break;
case "f":
rate = FULLSIZE;
break;
}
}
public int getRentalDays(){
return rentalDays;
}
public void setRentalDays(int days){
rentalDays = days;
}
public double getTotalCost(){
return totalCost;
}
public void setTotalCost(double cost){
totalCost = cost;
}
public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());
}
}
Run Code Online (Sandbox Code Playgroud)
子类LuxuryCarRental ....
public class LuxuryCarRental extends CarRental {
final double chauffeur = 200.00;
final double dailyRate = 79.99;
protected String chauffeurStatus;
public LuxuryCarRental(String renterName, int zipCode, String carSize, int rentalDays) {
super(renterName, zipCode, carSize, rentalDays);
}
public String getChauffeurStatus(){
return chauffeurStatus;
}
public void setChauffeurStatus(String driver){
chauffeurStatus = driver;
}
public double getChauffeurFee(){
return chauffeur;
}
public void setTotalLuxuryCost(){
if (chauffeurStatus=="y")
setTotalCost((dailyRate * getRentalDays()) + (chauffeur * getRentalDays()));
else
setTotalCost(dailyRate * getRentalDays());
}
@Override
public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Optional Chauffeur fee: $" + getChauffeurFee());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());
}
}
Run Code Online (Sandbox Code Playgroud)
现在使用main方法的类:
import java.util.Scanner;
public class UseRentalCar {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
CarRental rentalCar = new CarRental();
System.out.println("==========================");
System.out.println("RENTAL CAR SELECTION");
System.out.println("==========================");
System.out.println("Enter your name: ");
rentalCar.setRenterName(keyboard.next());
System.out.println("Enter your zip code: ");
rentalCar.setZipCode(keyboard.nextInt());
System.out.println("Enter the car size ("e=Economy, m=Midsize, f=Fullsize: ");
rentalCar.setCarSize(keyboard.next());
System.out.println("Enter the number of days: ");
rentalCar.setRentalDays(keyboard.nextInt());
rentalCar.displayRental();
}
}
Run Code Online (Sandbox Code Playgroud)
(省略一些原因没关系,主要是试图让对象实例化工作)
谢谢你的帮助!!
编译器抱怨CarRental构造函数需要四个参数(String,int,String和另一个int):
"CarRental类中的构造函数CarRental不能应用于给定类型; 必需:String,int,String,int found:无参数原因:实际和形式参数列表的长度不同"
但是在UseRentalCar中,你没有通过任何:
CarRental rentalCar = new CarRental();
"CarRental类中的构造函数CarRental不能应用于给定类型;必需:String,int,String,int found:无参数原因:实际和形式参数列表的长度不同"
如果您没有为您的类提供构造函数,Java将为您创建一个无参数构造函数.如果您提供自己的(在CarRental中使用4个参数),java将不会创建no-arg构造函数,因此您无法引用它.有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html.
您可以自己添加一个无参数构造函数,因为Java没有为您执行此操作.或者既然您为租车类提供了设置器,您可以使用类似于现在的设置器,并在CarRental(和LuxuryCarRental)中删除您的4-arg构造函数,并让Java添加默认值.
或者,如果由于某种原因想要保留这些构造函数,可以将用户输入保存在局部变量中,并将调用推迟到4-arg构造函数,直到完成所有用户输入.
在main方法中创建局部变量,比如String和int变量,然后在用这些变量填充用户输入之后,使用它们来调用构造函数.
我将发布一个通用示例,因为这是作业,最好向您展示概念,然后让您使用该概念来创建代码:
public class Foo {
private String name;
private int value;
public Foo(String name, int value) {
this.name = name;
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
别处
import java.util.Scanner;
public class Bar {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter name: ");
String name = keyboard.nextLine(); // local variable
System.out.print("Please enter value: " );
int number = keyboard.nextint(); // another local variable
keyboard.nextLine(); // to handle the end of line characters
// use local variables in constructor call
Foo foo = new Foo(name, number);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34235 次 |
| 最近记录: |