[英]Passing constructor parameter to method JAVA
目前,我正在通过Java学习软件开发课程。
我正在开发的程序(不用担心,可以让我获得外部帮助)给我带来了一个问题,而且还多了,所以问题是我不明白他们是如何表达眼前任务的。 这就是我的位置。
我需要做的是创建带有两个变量的构造函数,然后将参数之一传递给方法。 该类和构造函数称为Order()
,该方法称为testQuantity
。 这是我不明白的。 第一个参数productName
有它的值分配给一个实例变量productName
由代码this.productName = productName;
但是对于第二个参数quantity
它并没有要求我将值分配给实例变量quantity
,而是“将数量参数传递给testQuantity方法。”
好的,这是我到目前为止的代码,距离完成还很遥远,还有另外四个类尚未开始。 还可以随时指出您在此过程中发现的任何问题。 在此先感谢您的帮助。
package JaveGUIPrototype;
import java.util.Arrays;
/**
*
* @author User
*/
public class Order {
private String productName;
private double price;
private int discount;
private int quantity;
private double total;
private String message;
private boolean isDiscounted;
private boolean isValidOrder;
private static int orderNum = 0;
public Order() {
// sets validity to false, sets message, increments orderNum by 1
isValidOrder = false;
message = "**ERROR**: Order number cannot be totalled as no details have been supplied";
orderNum = orderNum + 1;
}
public Order(String productName, int quantity) {
// sets productName variable to productName parameter value
this.productName = productName;
this.quantity = quantity;
}
public void testQuantity() {
// tests the value of the quantity variable
// quantity is 0 or less, order isn't valid, message explains why
if(quantity <=0){
isValidOrder = false;
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
}
// quantity is greater than 1000, order isn't valid, message explains why
else if(quantity >1000) {
isValidOrder = false;
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
}
// quantity is valid, quantity instance variable assigned value of parameter variable
else {
this.quantity = quantity;
}
}
public void testDiscount(int discount) {
// tests the value of the discount variable
// discount is 0 or less, order isn't valid, message explains why
if(discount <=0) {
isValidOrder = false;
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
}
// discount is greater than 50, order isn't valid, message explains why
else if(discount >50) {
isValidOrder = false;
message = "**ERROR**: The discount rate cannot be greater than 50";
}
// discount is valid, discount instance variable assigned value of parameter variable
// isDiscounted variable set to true
else {
this.discount = discount;
isDiscounted = true;
}
}
String[] products = {"Compass",
"Eraser",
"Pen",
"Pencil",
"Pencil Case",
"Ruler",
"Scissors"};
double[] prices = {4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5};
int indexPos;
double indexVal;
public void getPrice(String productName) {
// searches products array by value of productName variable
indexPos = Arrays.binarySearch(products, productName);
// tests for validity of result
if (indexPos >-1) {
// assigns value of prices array by index position
indexVal = prices[indexPos];
// assigns prices array value to price variable
price = indexVal;
}
else {
// product name not valid, not a valid order, message explains why
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}
public void calculate() {
// tests for validity
if(isValidOrder == true) {
// tests for discount
if(isDiscounted == false) {
// equats total without discount
total = quantity * price;
}
else {
// equats total with discount
total = quantity * price - quantity * price * (discount/100);
}
}
}
public String getOrderDetails() {
//tests for validity
if(isValidOrder == true) {
//tests for discount
if(isDiscounted == false) {
// sets non discounted message details
message = "Order number: " + orderNum +
"Product name: " + productName +
"Product price: $" + price +
"Order quantity: " + quantity +
"Total price: $" + total;
}
else {
// sets discounted message details
message = "Order number: " + orderNum +
"Product name: " + productName +
"Product price: $" + price +
"Order quantity: " + quantity +
"Discount: " + discount + "%" +
"Total price: $" + total;
}
}
return message;
}
它不要求我将值分配给实例变量
quantity
那你为什么要那样做? 更改testQuantity
以接受参数,并传递构造函数的quantity
参数:
public Order(String productName, int quantity) {
this.productName = productName;
testQuantity(quantity);
}
public void testQuantity(int quantity) {
// Do something with quantity.
// Copying the original implementation should suffice.
}
您可以创建多个构造函数,只要它们具有不同的签名即可。 (不同的参数号,不同的参数类型),但不是返回类型。
您应该阅读有关方法重载的更多信息,但您需要:
public Order(String productName, int quantity) {
// sets productName variable to productName parameter value
this.productName = productName;
this.quantity = quantity;
}
public Order(String productName) {
// sets productName variable to productName parameter value
this.productName = productName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.