0

我是 java 的绝对初学者,需要帮助添加字符串实例变量。我什至真的不知道如何问我的问题,但是

public class Account {
  private int AccountNumber;
  private int FirstName;
  private int LastName;
// ok here is where im stuck, i realize that Firstname and LastName are not integers but how else can i make a instance variable of them?           
  private double balance;
 public Account(double initialBalance, int getFirstName, int getLastName, int        getAccountNumber) {
//Int is not the correct type for FirstName and LastName but I dont know what else to call it. Any ideas?
    FirstName = getFirstName;
    LastName = getLastName;
    AccountNumber = getAccountNumber;
    }
4

3 回答 3

0

您需要字段String的参数类型String

 public Account(double initialBalance, String firstName, String lastName, int accountNumber) {
    FirstName = firstName;
    LastName = lastName;
    AccountNumber = accountNumber;
}

也许您正在考虑 C 风格的指针,而 java 没有。

于 2013-09-25T00:38:36.167 回答
0

正如 Sotirios 提到的,像这样使用 String -

public class Account {
  private int AccountNumber;
  private String FirstName;
  private String LastName;
  private double balance;
  public Account(double initialBalance, String getFirstName, String getLastName, int getAccountNumber) {
   FirstName = getFirstName;
   LastName = getLastName;
   AccountNumber = getAccountNumber;
  }
}
于 2013-09-25T00:42:50.783 回答
0
public class Account {
  private int accountNumber;
  private String firstName;
  private String lastName;
  private double balance;
  public Account(double aInitialBalance, String aFirstName, String aLastName, int        aAccountNumber) {
    balance =aInitialBalance;
    firstName = aFirstName;
    lastName = aLastName;
    accountNumber = aAccountNumber;
 }
}

class  AccountTest{

 public static void main(String []args){
    Account a1=new Account(100.00,"Billy","Sims",1);
    System.out.println("FirstNmae:"+a1.firstName);
    System.out.println("LastNmae:"+a1.lastName);
    System.out.println("Balance:"+a1.balance);
    System.out.println("AccountNumber:"+a1.accountNumber);
 }
}
于 2013-09-25T00:46:54.880 回答