Java的新手,我对如何组合这两个对象感到困惑。抱歉,如果我不清楚/是否已被询问过。
我需要添加一个。PiggyBank到两个。
我们不允许更改用于输出代码的程序
public class PiggyBank {
doubles pennies, nickels, dimes, quarters, totalValue, bankTotal;
public PiggyBank(int p, int n, int d, int q)
{
pennies = p;
nickels = n;
dimes = d;
quarters = q;
totalValue = pennies + nickels + dimes + quarters;
}
public void addPenny()
{
}
//accessors
public double getP()
{
return pennies;
}
public double getN()
{
return nickels;
}
public double getD()
{
return dimes;
}
public double getQ()
{
return quarters;
}
public double combinePiggy(double bank2)
{
two.
bankTotal = bank1 + bank2;
}
public static void main(String[] args) {
PiggyBank one = new PiggyBank(5, 5, 5, 5);
PiggyBank two = new PiggyBank(2, 3, 4, 1);
System.out.println(“Account 1: “ + one + “\n”);
System.out.println(“Account 2: “ + two + “\n”);
one.combinePiggy(two);
System.out.println(“Account 1: “ + one + “\n”);
System.out.println(“Account 2: “ + two + “\n”);
}
}
您需要创建该combinePiggy(PiggyBank)
方法,然后将参数的值添加到调用此方法的相应值。
一些抽象和简化的示例可以帮助您入门(您应该自己做一件真正的事,以产生学习效果):
class Thing {
int x;
int y;
void combine(Thing otherThing) {
x += otherThing.x;
y += otherThing.y;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句