重载的java方法
//*******************************************************
// FlexibleAccount.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class FlexibleAccount
{
private double balance;
private String name;
private long acctNum;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public FlexibleAccount(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
public FlexibleAccount(double initBal, String owner, double number)
{
Random generator = new Random();
balance = initBal;
name = owner;
number=generator.nextDouble();
this.acctNum= number;
}
public FlexibleAccount(String owner)
{
balance = 0;
name=owner;
Random generator = new Random();
number=generator.nextDouble();
this.acctNum= number;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
public void withdraw(double amount,int fee)
{
if(balance>=amount)
balance-= amount+fee;
else
System.out.println("Insufficient funds");
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
这就是我所拥有的,我正试图使FlexibleAccount过载3倍,如下所示
- public FlexibleAccount(双初始值、字符串所有者、长编号)–根据指定初始化余额、所有者和帐号
- public FlexibleAccount(double initBal,String owner)–根据指定初始化余额和所有者;随机生成帐号李>
- public FlexibleAccount(字符串所有者)–根据指定初始化所有者;将初始余额设置为0并随机生成账号李>
当我编译时,我得到了这些错误
FlexibleAccount.java:31: possible loss of precision
found : double
required: long
this.acctNum= number;
^
FlexibleAccount.java:39: cannot find symbol
symbol : variable number
location: class FlexibleAccount
number=generator.nextDouble();
^
FlexibleAccount.java:40: cannot find symbol
symbol : variable number
location: class FlexibleAccount
this.acctNum= number;
^
我如何解决这个问题,这是过载的正确方法吗
# 1 楼答案
就像前面的问题一样-在
public FlexibleAccount(String owner)
中写入acctNum = number;
时,没有这样的变量number
:编辑: 这是您的第二个构造函数:
出于某种原因,您声明它有3个参数,尽管您编写了您希望它只接收2个参数-
我猜你想要一个可变的数字。。。应该是这样的:
正如你在上一篇文章中所说的。问题这是一个家庭作业,所以我不会再补充了。阅读到目前为止你得到的答案和评论,并进行整理