Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以getXXXX()开头的方法做什么?_Java_Methods_Return - Fatal编程技术网

Java 以getXXXX()开头的方法做什么?

Java 以getXXXX()开头的方法做什么?,java,methods,return,Java,Methods,Return,我必须在我的程序中使用这两种方法,但我不知道它们做什么,因为我的程序在没有它们的情况下按照我希望的方式工作,当我把它们放在代码中时,输出或任何东西都不会有什么不同 public double getPurchase() { return purchase; } public int getItems() { return numItems; } 以下是我的代码的其余部分: public class GroceryListIU extends javax.swing.JFra

我必须在我的程序中使用这两种方法,但我不知道它们做什么,因为我的程序在没有它们的情况下按照我希望的方式工作,当我把它们放在代码中时,输出或任何东西都不会有什么不同

 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);
        }
    }
}                                           
如何在我的程序中使用它们?

它们是。你可以在你的程序中使用它们,但你从未使用过它们

请注意,它们是
public
,而它们返回的变量应该是
private
。您正在通过公开您的数据成员来进行破坏

以这一类为例:

public class MyClass {
   private int    myPrivateInt;
   private String myPrivateString;

   public int getInt() {
       return myPrivateInt;
   }

   public String getString() {
       return myPrivateString;
   }
}

由于
myPricateInt
myPrivateString
private
,您无法从外部访问它们,因此我需要一个getter方法来获取这些变量。

这是
封装

如果您有这样的getter,那么字段上的
private
access修饰符将使它们更有意义

private double purchase;
private int numItems;
愚蠢的问题。 这些是变量purchase和nemItems的getter方法,它们是私有的。
Java中的访问器和变异器。这听起来很有道理。

他们是能手

gettersetter的要点是,只有它们才用于访问它们正在获取或设置的私有变量。通过这种方式,您可以提供封装,并且以后重构或修改代码会容易得多

短而甜的优点是

  • 用于可重用性
  • 在编程的后期阶段执行验证
  • Getter和setter方法是访问私有对象的公共接口 班级成员
  • 根据你方要求

    public double getPurchase() {
        return purchase;
    }
    
    public int getItems() {
        return numItems;
    }
    

    purchase
    numItems
    是私有的,因此您需要getter

    它们是设置和获取方法
    Public double getPurchase()
    返回类中的
    purchase
    变量,而
    Public int getItems()
    返回
    numItems
    变量。当您删除它们时,它不会影响代码的原因是因为您直接访问这些变量,因为它们是公共的。如果将变量设置为private,则必须使用这些方法

    比方说,我把一个盘子放在桌子上,然后我就再也不去想它,也不去用它。有什么问题吗?我在问我如何在我的程序中使用它们我一点都不喜欢这种问题:这个家伙来到这里,根本没有搜索过,似乎完全没有编程技能,问了一个愚蠢的问题,没关系。有人回答,得到5张赞成票,每个人都很高兴。最后,这对其他任何人都不会有用。感谢你的作品@OlivierH你的问题对很多人来说总是有趣/有用吗?虽然问题很弱,@Marounnaroun的答案很好。向上投票是为了向OP和网站的未来用户表明这一点。