简体   繁体   English

int定义-不确定代码的目的是什么

[英]int definition - not sure what the purpose is in the code

public void submitOrder(View view) {
    CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamCheckBox);
    boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
    Log.v("MainActivity", "Has whipped Cream: " + hasWhippedCream);

    int price = calculatePrice();
    String priceMessage = createOrderSummary(price, hasWhippedCream);
    displayMessage(priceMessage);
}
/**
 * Calculates the price of the order.
 *
 * @param 'quantity' is the number of cups of coffee ordered
 * "5" is used for the price of each coffee
 * @return total price
 */
private int calculatePrice() {
    int price = quantity *5;
    return price;
}

I am very new to coding, so please bear in mind this is something new, and I'm trying to understand how this works.. I'm taking this course, and I ended up getting the code above. 我对编码非常陌生,因此请记住这是新事物,并且我试图了解其工作原理。.我正在学习本课程,最终得到了上面的代码。

I am questioning why I have "calculatePrice" int because all I need is int "price" that is defined by quantity and stores the value of price. 我在问为什么要有“ calculatePrice” int,因为我所需要的只是由数量定义并存储价格值的int“ price”。 This is what I am understanding based on "private int calculatePrice" 3 lines. 这是我根据“ private int CalculationPrice” 3行得出的理解。 What is the purpose of having "int price = calculatePrice();" 具有“整数价格=计算价格();”的目的是什么? in the public void? 在公共场合无效? I feel like I defined "price" within private "calculatePrice," and now I am RE-DEFINING "price" by writing "int price = calculatePrice();". 我感觉自己在私有的“ calculatePrice”中定义了“ price”,现在我通过编写“ int price = calculatePrice();”来重新定义“ price”。 It's confusing. 令人困惑。 Could someone explain why I "int price" is defined twice, meaning defined within "calculatePrice" and re-defined by "calculatePrice" again in the public void? 有人可以解释为什么我的“ int price”被定义了两次,这意味着在“ calculatePrice”中定义了,又在公共场合再次由“ calculatePrice”重新定义了?

I'm having hard time getting the concept... thanks for helping out! 我很难理解这个概念...感谢您的帮助!

In ref to the above code, int price defined two times in two methods whose scope very much ends in the methods they are defined. 在上面的代码中,int price在两个方法中定义了两次,它们的范围在它们定义的方法中大都结束了。 Though calculatePrice method in your code does not do much, but in real projects, you can assign a complex method which executes lots of logic and finally returns evaluated value in the form of int. 尽管代码中的calculatePrice方法作用不大,但是在实际项目中,您可以分配一个复杂的方法,该方法执行大量逻辑并最终以int形式返回评估值。

You are not re-defining the variable price. 您没有重新定义可变价格。 Both "Price" variables are in different scopes, so actually they are not the same variable. 两个“价格”变量都在不同的范围内,因此实际上它们不是同一变量。 In the first case, price is only visible and accessible from the submitOrder method since it's declared inside that method. 在第一种情况下, price仅在submitOrder方法中可见并可以访问,因为它是在该方法中声明的。 In the second case, price is only visible and accesible from calculatePrice method, so even having the same name they are totally different variables. 在第二种情况下, price只能从calculatePrice方法中看到和访问,因此即使名称相同,它们也是完全不同的变量。

You can read more about scope and lifetime of variables in this link: http://www.javawithus.com/tutorial/scope-and-lifetime-of-variables 您可以在此链接中了解有关变量范围和生存期的更多信息: http : //www.javawithus.com/tutorial/scope-and-lifetime-of-variables

By the way, I think the method calculatePrice is not properly defined, it needs a int parameter to setthe quantity: 顺便说一句,我认为方法calculatePrice没有正确定义,它需要一个int参数来设置数量:

private int calculatePrice(int quantity) {
    int price = quantity *5;
    return price;
}

Once you have modified this method, you should call it from the method submitOrder in this way: 修改此方法后,应以这种方式从方法submitOrder调用它:

 int price = calculatePrice("a number");

The second method : 第二种方法:

private int calculatePrice() { int price = quantity *5; return price; } private int calculatePrice() { int price = quantity *5; return price; } this is a method with returning type of integer, it is responsible for calculate the price and return the result which is price. private int calculatePrice() { int price = quantity *5; return price; }这是一个返回整数类型的方法,它负责计算价格并返回价格结果。 in your first method you call this function and get the result of your price and store it in your actual variable which is also named price. 在第一个方法中,您调用此函数并获取价格结果,并将其存储在实际变量中,该变量也称为price。 to not be confused you can write the second method as : 为了避免混淆,您可以将第二种方法编写为:

private int calculatePrice() { return quantity *5; } private int calculatePrice() { return quantity *5; } and then call it in the first method as int price=calculatePrice() private int calculatePrice() { return quantity *5; } ,然后在第一个方法中将其调用为int price=calculatePrice()

The calculatePrice() is a function which returns an integer value return price . calculatePrice()是一个返回整数值return price的函数。 So when you call that function it will execute that part of code inside calculatePrice() and will return an answer which is integer. 因此,当您调用该函数时,它将在calculatePrice()执行该部分代码,并将返回整数的答案。

The scope of the variable int price which is defined inside the function is only limited to that function (you can name your variable as you like, it is not necessary to name only "price"). 在函数内部定义的变量int price的范围仅限于该函数(您可以根据需要命名变量,不必仅命名“ price”)。

The int price = calculatePrice(); int price = calculatePrice(); is another variable which you are assigning value which is returned from your function. 是另一个您要为其赋值的变量,该变量从函数返回。

The methods after modifying the variable name... 修改变量名称后的方法...

/**
 * Calculates the TOTAL price of the order.
 */
private int calculatePrice(int qty) {
    int totalPrice = quantity * qty;
    return totalPrice;
}

// You are calling by passing the quantity (qty)
int qty = 5;
int invoicePrice = calculatePrice(qty);
    String priceMessage = createOrderSummary(inoicePrice, hasWhippedCream);

Hope this will help to understand the code. 希望这将有助于理解代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM