何时使用构造函数?

时间:2013-08-25 14:57:16

标签: c#

我正在网上学习c#,我刚刚完成了一个练习,我应该创建一个名为“People”的类,并创建5个可以让人们独立的变量:

   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;

然后,我得到了一个问题:“如果您认为银行类与一个人(人类)相关联,您如何使用班级中的”人“类作为”银行“?

现在我显然不明白是什么东西......有什么想法吗?

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

4 个答案:

答案 0 :(得分:2)

可能很简单:

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

答案 1 :(得分:2)

那不是构造函数,它试图告诉你,你可以在你创建的另一个类中创建属性作为属性。

在他们的示例中,每个银行都有一个人,因此您可以将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

答案 2 :(得分:0)

怎么样

public class Person
{
    //A property for Name

    //A property for Address 
}

在另一个类中,收集Persons

的属性
public List<Person> People { get; set; }

答案 3 :(得分:0)

这就是我要去的方式:

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

请始终从属性,方法等中记下访问参数。