要求不同数量的构造函数的属性

时间:2017-02-13 15:14:03

标签: c# constructor propertyinfo bank

我有一个简短的问题。我有一个类Bank,需要具有不同数量的构造函数的属性。

class Program
{
    static void Main(string[] args)
    {
        Bank ing = new Bank("ing");
        int AccountNrName1 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
        int AccountNrName2 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
}

构造函数“OpenAccount”必须返回两种情况(AccountNrName1,AccountNrName2)。以下是我到目前为止的情况:

public class Bank
{
    private string name, lastname, housenumber, postalcode;
    private int openaccount;
    private int balance;
    private int min;

    public int OpenAccount(string Name, string LastName, string HouseNumber, string PostalCode, int Balance, int Min)
    {
        name = Name;
        lastname = LastName;
        housenumber = HouseNumber;
        postalcode = PostalCode;
        balance = Balance;
        min = Min;

        return openaccount;
    }
}

2 个答案:

答案 0 :(得分:0)

方法可以有可选参数,但是,你也可以重载 - 所以,虽然你似乎想要一个构造函数(因为你正在创建对象,但是,你可以使它成为方法..但你需要做首先是对象!):

可能你可以有2个构造函数来允许这些条件:

public OpenAccount (String name, String LastName, String Housenumber, String Postcode)
{ do stuff }
public OpenAccount (String name, String LastName, String Housenumber, String Postcode, int balance, int min)
{ do stuff }

或者你可以做到

public OpenAccount (String name, String LastName, String Housenumber, String Postcode, int balance=0, int min=0)
{do stuff}

允许您自动设置这些值(如果未提供)。我没有检查它是否适用于构造函数,但它应该。

答案 1 :(得分:0)

您需要一个班级来保留帐户信息。

public class Account {

    public Account(int accountNumber) { AccountNumber = accountNumber; }

    public int AccountNumber { get; private set; }
    public string Name { get;set; } 
    public string LastName { get;set; } 
    public string HouseNumber { get;set; }
    public string PostalCode { get;set; }
    public int Balance { get;set; }
    public int Min { get;set; }
}

Bank将包含一系列帐户,并允许您打开帐户

public class Bank {

    string name;
    int accountIds = 0;
    List<Account> accounts = new List<Account>();

    public Bank(string bankName) {
        name = bankName;
    }

    //Open an account and return the account number
    //Using optional parameters to allow
    //OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
    //OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
    public Account OpenAccount(string Name, string LastName, string HouseNumber, string PostalCode, int Balance = 0, int Min = 0) {
        var newAccount = new Account(++accountIds) {
            Name = Name;
            LastName = LastName;
            HouseNumber = HouseNumber;
            PostalCode = PostalCode;
            Balance = Balance;
            Min = Min;
        };
        accounts.Add(newAccount);
        return newAccount;    
    }
}

现在,当您致电银行开户时

class Program {
    static void Main(string[] args) {
        Bank ing = new Bank("ing");
        Account AccountNrName1 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode");
        Account AccountNrName2 = ing.OpenAccount("Name", "LastName", "HouseNumber", "PostalCode", Balance, Minimum);
    }
}

您应该返回已填充所有属性的已开设帐户。