在Java中使用getter和setter方法
尝试创建一个使用用户输入和已知信息来确定每天租车成本的项目。我遇到的问题是CarRental类似乎没有从测试类获取输入。不知道怎么解决 class CarRental在Java中使用getter和setter方法,java,getter-setter,Java,Getter Setter,尝试创建一个使用用户输入和已知信息来确定每天租车成本的项目。我遇到的问题是CarRental类似乎没有从测试类获取输入。不知道怎么解决 class CarRental public class CarRental { private String customerName; private String ratingKey; private double baseFactor; private double basefactor; private double fudge; private d
public class CarRental {
private String customerName;
private String ratingKey;
private double baseFactor;
private double basefactor;
private double fudge;
private double computeDailyFee;
public CarRental(String customerName, String ratingKey, double baseFactor, double fudge, double computeDailyFee) {
this.customerName = customerName;
this.ratingKey = ratingKey;
this.baseFactor = baseFactor;
this.basefactor = basefactor;
this.fudge = fudge;
this.computeDailyFee = computeDailyFee;
//intitialize
}
public String getcustomerName() {
return this.customerName;
}
public String getratingKey() {
return ratingKey;
}
public void setcustomerName(String newCustomerName) {
customerName = newCustomerName;
System.out.println(customerName);
}
public void setratingKey(String newRatingKey) {
ratingKey = newRatingKey;
System.out.println(ratingKey);
}
public double baseFactor() {
switch (ratingKey.charAt(0)) {
case 'S':
baseFactor = 1.0;
break;
case 'C':
baseFactor = 1.2;
break;
case 'U':
baseFactor = 1.4;
break;
case 'T':
baseFactor = 1.6;
break;
case 'B':
baseFactor = 2.0;
break;
default:
System.out.println("Invalid type");
break;
}
System.out.println(baseFactor);
return baseFactor;
}
public double fudge() {
switch (ratingKey.charAt(1)) {
case 'N':
fudge = 11.40;
break;
case 'V':
fudge = 0;
break;
default:
System.out.println("Invalid type");
break;
}
System.out.println(fudge);
return fudge;
}
public double computeDailyFee() {
computeDailyFee = (baseFactor() * (89.22 - fudge()));
System.out.println(computeDailyFee);
return computeDailyFee;
}
}
等级租金测试
public class RentalTest {
public static void main(String[] args) {
CarRental rental = new CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
String newCustomerName = rental.getcustomerName();
String newCustomerName = JOptionPane.showInputDialog("Enter your name");
//System.out.println(newCustomerName);
RentalTest rentaltest = new RentalTest();
JOptionPane.showMessageDialog(null, "Input code for model, then code for condition (No Spaces)");
String newRatingKey = JOptionPane.showInputDialog(
"Models: 'S' = Sub-compact 'C'=Car 'U'=SUV 'T'=Truck 'B'=Bigfoot\n"
+ "Condition: 'N'= New 'V' = Vintage");
newRatingKey = newRatingKey.toUpperCase();
//System.out.println(newRatingKey);
}
}
你可以试试这样的东西
CarRental carRental = CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
carRental.getcustomerName();
您需要实际构造一个CarRental,然后获取名称。在
main
中:
// Build the object
CarRental rental = new CarRental("first", "last", 1.0, 1.0, 100.0);
// Now we can call instance methods.
System.out.println(rental.getCustomerName());
这个程序中有很多错误,请尝试重新查看代码,因为您的两个类都声明为public,因为它不能在单个java程序中工作,并且
String newCustomerName = getcustomerName;
上述代码将给出编译错误
这里我认为getCustomerName是一个方法,所以应该像下面这样声明它
getcustomerName();
要访问它,请尝试创建carRental类的对象,甚至您还没有通过在构造函数中传递值来初始化carRental类的实例成员 您对
字符串newCustomerName=getcustomerName有问题代码>。这是编译吗?您从未声明和实例化过CarRental
对象。这应该是主方法中唯一的字段。您能给我们编译但运行不正确的实际代码吗?我不能让你的代码在这里编译。这甚至不会编译。我真的不知道如何在不同的类中使用这些方法,我只是尝试调用get方法。不起作用。在他的例子中,CarRental
,而不是RentalTest
,有getCustomerName
。是的,我编辑了我的答案,谢谢@slitlycubandarn,我刚才也回答了…我现在已经添加了这个,但是,它显示了一个错误,即newCustomerName已经存在defined@user2854982根据你的第一次编辑,我认为这是你的实例。您已经在其他地方声明了newCustomerName
,这就是全部。Nvm,修复了该部分。但是,在我的CarRental类中,它似乎仍然没有接收到这些字符串。@user2854982,String
s正在构造函数中设置。你需要仔细地一步一步地检查你的代码,弄清楚发生了什么。修复了它。这就是全部。租赁。设置客户名称(新客户名称);租赁。设置键(新建键);基本因子();租金。福吉();rental.computeDailyFee();谢谢,我现在已经包括在内了。它仍然不能正常工作,我真的不明白到底出了什么问题。