【问题标题】:What the methods do which starts with getXXXX()? [closed]以 getXXXX() 开头的方法是什么? [关闭]
【发布时间】:2013-12-13 04:07:10
【问题描述】:

我必须在我的程序中使用这两种方法,但我不知道它们的作用,因为我的程序在没有这些的情况下按照我想要的方式工作,并且当我将它们放入我的代码中时,它不会对输出或任何东西。

 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);
        }
    }
}                                           

如何在我的程序中使用它们?

【问题讨论】:

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

标签: java methods return


【解决方案1】:

这些是getters。您的程序中可能有它们,但您从未使用过它们。

注意它们是public,而它们返回的变量应该是private。你通过暴露你的数据成员来破坏encapsulation

考虑这个类:

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

   public int getInt() {
       return myPrivateInt;
   }

   public String getString() {
       return myPrivateString;
   }
}

由于myPricateIntmyPrivateStringprivate,你不能从外部访问它们,这就是为什么我需要一个getter 方法来获取这些变量。

【讨论】:

    【解决方案2】:

    他们是吸气剂

    getterssetters 的意义在于,只有它们才能用于访问它们正在获取或设置的私有变量。这样您就可以提供封装,以后重构或修改代码会容易得多。

    短而甜的优点是

    1. 为了可重用性。
    2. 在编程的后期执行验证。
    3. Getter 和 setter 方法是访问私有的公共接口 班级成员

    根据你的问题

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

    purchasenumItems 是私有的,所以你需要 getter

    【讨论】:

      【解决方案3】:

      这是encapsulation

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

      private double purchase;
      private int numItems;
      

      【讨论】:

        【解决方案4】:

        愚蠢的问题。 这些是变量 purchase 和 nemItems 的 getter 方法,它们是私有的。 Java 中的访问器和修改器。会响铃吗?

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-11-19
            • 2012-01-19
            • 2012-05-06
            • 2014-05-17
            • 2011-04-26
            • 2014-09-12
            • 2019-07-08
            • 2013-08-11
            相关资源
            最近更新 更多