我必须在我的程序中使用这两种方法,但我不知道他们做了什么,因为我的程序按照我想要的方式工作而没有这些,当我把它们放在我的代码中时它没有区别于输出或任何东西。
public double getPurchase() {
return purchase;
}
public int getItems() {
return numItems;
}
以下是我的其余代码:
public class GroceryListIU extends javax.swing.JFrame {
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
public double itemPrice;
public final double SALES_TAX = 0.065;
public double totalPrice;
public double tax;
public double purchase;
public int numItems;
/**
* Creates new form GroceryListIU
*/
public GroceryListIU() {
initComponents();
//delcares purchase and numItems and resets them to 0
purchase = 0;
numItems = 0;
}
//set method to add item price
public void recordPurchase(double itemPrice) {
purchase = purchase + itemPrice;
numItems++;
}
public double getPurchase() {
return purchase;
}
public int getItems() {
return numItems;
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
//clicking exit ends the program
System.exit(0);
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
//When the user clicks "reset" all variables are set to blank or 0
txtItemPrice.setText("");
txtSubTotal.setText("");
txtNumberOfItems.setText("");
txtSalesTax.setText("");
txtTotalPrice.setText("");
numItems = 0;
purchase = 0;
}
private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) {
boolean keepShopping = true;
JFrame frame = new JFrame();
while (keepShopping) {
try {
//When the user clicks "checkout" a input dialog will appear to enter the item price
String newItemPrice = JOptionPane.showInputDialog(frame,
"Enter Item Price",
"Enter Price",
JOptionPane.PLAIN_MESSAGE);
//if the user clicks cancel or clicks OK and left the text field blank, calculations will be made
if ((newItemPrice != null) && (newItemPrice.length() > 0)) {
//parse the double item price
itemPrice = Double.parseDouble(newItemPrice);
//takes itemPrice and plugs it into recordPurchase method
recordPurchase(itemPrice);
//adds 1 to txtNumberOfItems each time the user enters a number until it ends
txtNumberOfItems.setText((numItems) + "");
//adds item price to the txtItemPrice text field
txtItemPrice.setText(defaultFormat.format(itemPrice));
//adds the sub total to the txtSubTotal text field
txtSubTotal.setText(defaultFormat.format(purchase));
} else {
//when the user clicks ok when blank or cancel the program does the rest of the calculations
keepShopping = false;
//tax is 6.5%, you would multiply that by the purchase total
tax = SALES_TAX * purchase;
//sets "tax" in the txtSalesTax text field
txtSalesTax.setText(defaultFormat.format(tax));
//the total price is tax plus the sub total
totalPrice = tax + purchase;
//add the total price to the totalPrice text field
txtTotalPrice.setText(defaultFormat.format(totalPrice));
}
} catch (NumberFormatException e) { //if the user enters string data, an error will appear
JOptionPane.showMessageDialog(frame,
"You must enter positive numerical data!",
"Bad Data!",
JOptionPane.ERROR_MESSAGE);
}
}
}
如何在我的程序中使用它们?
答案 0 :(得分:4)
Therese getters。您可能在程序中使用它们,但从未使用它们。
请注意,它们是public
,而它们返回的变量应该是private
。您通过公开数据成员来破解encapsulation。
考虑这个课程:
public class MyClass {
private int myPrivateInt;
private String myPrivateString;
public int getInt() {
return myPrivateInt;
}
public String getString() {
return myPrivateString;
}
}
由于myPricateInt
和myPrivateString
是private
,您无法从外部访问它们,这就是我需要 getter 方法来获取这些变量的原因。
答案 1 :(得分:1)
他们是 getters
getters 和 setters 的意思是,只有它们才能用于访问他们正在获取或设置的私有变量。通过这种方式,您可以提供封装,以后可以更轻松地重构或修改代码。
短暂而甜蜜的优势是
根据你的问题
public double getPurchase() {
return purchase;
}
public int getItems() {
return numItems;
}
purchase
和numItems
是私密的,所以你需要吸气剂
答案 2 :(得分:0)
这是encapsulation
。
如果你有这样的getter,那么你的字段上的private
访问修饰符会使它们更有意义。
private double purchase;
private int numItems;
答案 3 :(得分:0)
愚蠢的问题。 这些是变量购买的getter方法和nemItems,它们是私有的。 Java中的访问器和变量器。它响了吗。
答案 4 :(得分:0)
设置并获取方法。 Public double getPurchase()
会从类中返回purchase
变量,public int getItems()
会返回numItems
变量。它在删除代码时不会影响您的代码的原因是因为您直接访问这些变量是因为它们是公共的。如果将变量设置为private,则必须使用这些方法。