需要帮助在两个班级之间制作一个列表

本文关键字:之间 列表 一个 两个 帮助 | 更新日期: 2023-09-27 18:01:43

基本上,我正在编写一个程序,该程序应该复制银行程序。有两个类,User和Account。我在用户帐户中制作了一个列表,其中包含该用户的所有帐户。该列表存储由Account类构造函数收集的数据。我花了很多时间阅读其他人类似的问题,这让我达到了这一点,但我基本上卡住了,希望有人能给我一些建议。我正试图实现在用户类中创建的列表在帐户类中工作,并认为我可能需要使用接口或子类。

    class User 
{
    //fields
    private int id;
    public List<Account> _List;

    //properties
    public int Id 
    {
        get { return id; }
    }
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public List<Account> Acc 
    {
        get { return _List; } 
        set { _List = value; } 
    }
    //constructors
    public User()
    {
        _List = new List<Account>();
        Acc = new List<Account>();
    }
    public User(int ident, string name, string username, string password, string account)
    {
        id = ident;
        Name = name;
        Username = username;
        Password = password;
        account = Acc.ToString();
    }

    // methods 
    public void AddAccount()
    {

        Account a = new Account();
        a.Acc = new List<Account>();
        a.Acc.Add(new Account(11111, "Primary Checking Account", 22, 23, "" ));
        a.Acc.Add(new Account(12345, "Primary Savings Account", 22, 23, ""));
        a.Acc.Add(new Account(04587, "Secondary Checking Account", 22, 23,""));
        a.Acc.Add(new Account(00222, "Secondary Savings Account", 22, 23, ""));
        a.Acc.Add(new Account(12457, "College Savings Account", 22, 23, "" ));
        a.Acc.Add(new Account(87442, "Rainy day Checking Account", 22, 23, ""));
    }
    public void DeleteAccount()
    {
    }
    public void ObjectAccount()
    {
    }
    public override string ToString()
    {
        return "null";
    }

这是第二类

     class Account
{
    //fields
    private int id;
    private string name;
    //properties
    public int Id 
    {
        get { return id; }
    }
    public string Name 
    {
        get { return name; } 
    }
    public int InitialBalance { get; set; }
    public int InterestRate { get; set; }
    public List<Account> Acc
    {
        get { return _List; } // this is where i am having the problem. I cant get the list called _List to exist in the account class.
        set { _List = value; }
    }
    //Constructors
    public Account() 
    {
        _List = new List<Account>();
        Acc = new List<Account>();
    }// this is so the constructor can take 0 arguements.
    public Account(int ident, string nam, int initialBalance, int interestRate, string account)
    {
        name = nam;
        id = ident;
        InitialBalance = initialBalance;
        InterestRate = interestRate;
        account = Acc.ToString();
    }
    public override string ToString()
    {
        return "null";
    }

谢谢你的时间!

需要帮助在两个班级之间制作一个列表

我想不出一个很好的理由,为什么你的Account对象需要访问属于特定User对象的帐户列表

如果您需要相关帐户的信息来执行某些操作,我可以

  • 从用户的角度执行这些操作(您已经可以访问帐户列表- _List),或者
  • 在你的User类上创建一个公共方法,这样你的帐户对象就可以请求一个相关帐户的列表。
    • 这对我来说似乎不是很直观,但我不知道你的完整用例。