我一直在尝试通过阅读“Java如何编写早期对象”来了解如何从同一个类的另一个方法内部调用方法。
我现在完全迷失了,书中使用的类比可以很容易地看到正在发生的事情。但是,将其转换为代码具有挑战性。
我已多次尝试解决这个问题并且已经做到了这一点: P.S。 为了简单起见,我排除了我认为对我的问题不重要的代码......
import java.util.Scanner;
public class BuyAHouseInc
{
private int housePrice;
private int amountOfHouses;
private int houseCounter;
// method to enter the amount of houses on sale
public void setAmountOfHouses()
{
// do stuff etc.
}
// method that sets the house price
public void setHousePrice()
{
// do stuff etc.
calculateFees(this.housePrice); // this is where I'm not sure...
}
//method to calculate fees and taxes
public void calculateFees(int housePrice) // does this receive this.housePrice?
{
// do some stuff
}
测试人员代码:
public class BuyAHouseIncTester
{
public static void main(String[] args)
{
BuyAHouseInc client1 = new BuyAHouseInc("John","Doyle","15 Newton Drive\nDublin 5\n", 550000) // Create object
// set amount of houses on sale to client 1
client1.setAmountOfHouses();
// set house price for each of the houses added to database
client1.setHousePrice();
}
}
在另一个方法中调用方法的代码是什么?是否会调用每个房价的价值?
答案 0 :(得分:2)
您可以简单地致电calculateFees(housePrice);
,因为在通话时可见的唯一housePrice
变量为instance variable
private int housePrice;
假设您根据创建constructor
housePrice
来设置BuyAHouseInc
//计算费用和税收的方法 public void calculateFees(int housePrice)//这会收到this.housePrice吗? { //做一些事情 }
是的,这将通过housePrice
calculateFees(housePrice);
calculateFees(int housePrice)
上面定义的局部变量仅在calculateFees(int housePrice){...}
方法
Passing Information to a Method or a Constructor
更新:根据评论,您需要更新您的setter以传递房价
public void setHousePrice(int housePrice)
{
this.housePrice = housePrice;
calculateFees(this.housePrice);
}
答案 1 :(得分:1)
是的,您的假设是正确的。
然而,有一些重要的事项需要注意。这段代码:
react-rails
Java是按值传递的。您已经传递了housePrice的值,但您没有修改原来的housePrice。
所以如果你做某些事情"并修改传入的housePrice变量,除非您在calculateFees中设置housePrice,否则不会保存这些更改。
在这种情况下,最好通过getter / setter操纵任何成员变量。顺便说一下,惯例是,设定者应该总是取值。这并不是随意的,因为许多库依赖于以某种方式起作用的set / get方法。
我已经对上述答案进行了更清晰的编辑。
答案 2 :(得分:1)
如果我错了,请纠正我,但这是一个可能的解决方案:
public void setHousePrice(int price)
{
calculateFees(price);
}
public void calculateFees(int housePrice)
{
// stuff with housePrice
}
然后做
variable.setHousePrice(variable.getHousePrice);
答案 3 :(得分:0)
public class BuyAHouseInc {
private double housePrice;
private int amountOfHouses;
private int houseCounter;
public BuyAHouseInc(double housePrice, int amountOfHouses, int houseCounter) {
this.housePrice = housePrice;
this.amountOfHouses = amountOfHouses;
this.houseCounter = houseCounter;
}
// method that sets the house price
public void generateHousePrice() {
// do stuff etc.
// you could also put the logic in the set method... Had to change the method name to
// generate as the getters and setter where created and the setter method is named
// exactly like your previous method
calculateFees(housePrice); // this is were I'm not sure...
}
// method to calculate fees and taxes
public void calculateFees(double housePrice) // does this receive
// this.housePrice?
{
// do some stuff
}
public double getHousePrice() {
return housePrice;
}
public void setHousePrice(double housePrice) {
this.housePrice = housePrice;
}
public int getAmountOfHouses() {
return amountOfHouses;
}
public void setAmountOfHouses(int amountOfHouses) {
this.amountOfHouses = amountOfHouses;
}
public int getHouseCounter() {
return houseCounter;
}
public void setHouseCounter(int houseCounter) {
this.houseCounter = houseCounter;
}
@Override
public String toString() {
return "BuyAHouseInc [housePrice=" + housePrice + ", amountOfHouses=" + amountOfHouses + ", houseCounter="
+ houseCounter + "]";
}
}
将两个类分隔在不同的文件中。另一件需要指出的是使用double代替int而不是int。 您还必须创建getter和setter才能获取和设置属性。 您还必须在类中添加构造函数。然后,您可以在main中创建该类的实例并调用其方法。 主要方法如下所示。
public class Main {
public static void main(String... args) {
BuyAHouseInc test = new BuyAHouseInc(200000, 4, 2);
test.setAmountOfHouses(6); // changed from 4 to 6
test.generateHousePrice();
test.calculateFees(test.getHousePrice());
test.toString();
}
}
希望你觉得这很有帮助,祝你好运!我编辑了代码并添加了toString方法以将信息输出为字符串。