添加两个包含姓名和帐号的实例变量?

时间:2013-09-25 00:33:50

标签: java string instance-variables

我是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;
    }

3 个答案:

答案 0 :(得分:0)

您想要String字段的String参数类型:

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

也许你正在考虑C风格的指针,这是java没有的。

答案 1 :(得分:0)

正如Sotirios所提到的,使用像这样的字符串 -

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;
  }
}

答案 2 :(得分: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);
 }
}