何时使用构造函数

本文关键字:构造函数 何时使 | 更新日期: 2023-09-27 18:28:53

我在线学习 c#,我刚刚完成了一个练习,如果我应该创建一个名为"People"的类并创建 5 个变量,这些变量可以使人们 uniqe:

   public string name;
    public int age;
    public double heigth;
    public double weigth;
    public string nationality;
    public int shoeSize;

我还创建了一个名为"Bank"的类,并删除了4个成员:

    int accountNumber;
    string firstName;
    string lastName;
    string bankName;

然后,我得到了一个问题:"如果你认为银行类与一个人(人物类(相关联,你会如何在"银行"类中使用类"人"?

现在我显然不明白在想什么。有什么想法吗?

编辑:什么时候需要构造函数方法?

何时使用构造函数

这不是一个构造函数,它试图教你,你可以将你创建的类作为你创建的另一个类中的属性。

在他们的示例中,每个银行都有一个人,因此您可以将 People 类作为名为 Person 的属性来表示帐户属于谁。您可以通过向Bank类添加以下内容来执行此操作:

public People person { get; set; }

就构造函数而言,如果要设置一些默认属性,则需要一个构造函数。考虑以下构造函数Bank

public Bank()
{
    accountNumber = 1;
    firstName = "Default";
    lastName = "Default";
    bankName = "Default";
    person = new People();
}

看到最后一行创建person了吗?如果你删除了它,但后来尝试做this.person.name你会得到一个NullReferenceException。这是因为默认情况下,您的person的值为 null

它可以像这样简单:

public class People // I would call it person though, as People is plural
{
    public int age;
    public double heigth;
    public double weigth;
    public string nationality;
    public int shoeSize;
}
public class Bank // I would call it BankAccount though
{
    int accountNumber;
    string firstName;
    string lastName;
    string bankName;
    // The answer to the question:
    People owner; // <-- Here the bank account has a reference to the People class, 
                  // you provided in the constructor
    // And if you need the constructor
    public Bank(People owner, int accountNumber)// <-- This is the constructor
    {
        this.accountNumber = accountNumber;
        this.owner = owner;
    } // <-- The constructor ends here.
}

怎么样

public class Person
{
    //A property for Name
    //A property for Address 
}

在另一个类中,用于收集Persons的属性

public List<Person> People { get; set; }
这就是

我会走的路:

public class Person
{
    public int Age { get; set; } // I would use properties and public properties are 
                                 // starting with a great letter
    public double Heigth { get; set; }
    public double Weigth { get; set; }
    public string Nationality { get; set; }
    public int ShoeSize { get; set; }
}
public class BankAccount
{
    private Person _person; // private field for the person object
    public int AccountNumber { get; private set; } // public propertie for the account 
                                                   // number with a private setter 
                                                   // because normally you want to read
                                                   // that from the outside but not set
                                                   // from the outside
    public string FirstName
    {
        get { return _person.FirstName; }
    }
    public string LastName;
    {
        get { return _person.LastName; }
    }
    public string BankName { get; set; }
    public Bank(Person person, int accountNumber)
    {
        AccountNumber = accountNumber;
        _person = person;
    }
}

请全部记下属性、方法等的访问参数。