-1

我在网上学习 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;

然后,我收到了一个问题:“如果您认为银行类与一个人(People 类)相关联,您将如何在“Bank”类中使用“People”类?

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

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

4

4 回答 4

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.
}
于 2013-08-25T15:00:55.673 回答
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.

于 2013-08-25T15:03:30.947 回答
0

怎么样

public class Person
{
    //A property for Name

    //A property for Address 
}

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

public List<Person> People { get; set; }
于 2013-08-25T15:03:55.843 回答
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;
    }
}

请务必记下来自属性、方法等的访问参数。

于 2013-08-25T15:24:51.750 回答