清单<T> working

已加入
2020年3月31日
留言内容
21
编程经验
Beginner
我正在学习c#,并从docs.microsoft.com跟随一个示例,链接如下

类和对象-C#教程简介

代码在下面
Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classes
{
    class Program
    {
        static void Main(string[] args)
        {
            var Client1 = new BankAccounts("ABC", 1000);
            var Client2 = new BankAccounts("XYZ", 500);

            Console.WriteLine($"Account {Client1.Number} was created for {Client1.Owner} with {Client1.Balance} initial balance.");
            Console.WriteLine($"Account {Client2.Number} was created for {Client2.Owner} with {Client2.Balance} initial balance.");
            Console.WriteLine();

            Client1.MakeWithdrawal(500, DateTime.Now, "Rent Payment");
            Console.WriteLine(Client1.Balance);
            Client1.MakeDeposit(100, DateTime.Now, "Friend paid me back");
            Console.WriteLine(Client1.Balance);
            Console.WriteLine();

            Client2.MakeWithdrawal(500, DateTime.Now, "Fuel Charges");
            Console.WriteLine(Client2.Balance);
            Client2.MakeDeposit(100, DateTime.Now, "Bonus");
            Console.WriteLine(Client2.Balance);
            
            Console.WriteLine(Client1.GetAccountHistory());
            Console.WriteLine();
            Console.WriteLine(Client2.GetAccountHistory());

            Console.ReadLine();
        }
    }
}

BankAccount Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classes
{
    class BankAccounts
    {
        private static int accountNumberSeed = 1234567890;

        public string Number { get; }
        public string Owner { get; set; }
        public decimal Balance
        {
            get
            {
                decimal balance = 0;
                foreach(var item in all交易s)
                {
                    balance += item.Amount;
                }
                return balance;
            }
        }

        public 清单<Transaction> all交易s = new 清单<Transaction>();

        public BankAccounts(string name, decimal initialBalance)
        {
            Number = accountNumberSeed.ToString();
            accountNumberSeed++;

            Owner = name;
            MakeDeposit(initialBalance, DateTime.Now, "Initial Balance");
        }

        public void MakeDeposit(decimal amount, DateTime date, string note)
        {
            if(amount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
            }
            var deposit = new 交易(amount, date, note);
            all交易s.Add(deposit);
        }

        public void MakeWithdrawal(decimal amount, DateTime date, string note)
        {
            if(amount <=0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
            }
            if(Balance - amount < 0)
            {
                throw new InvalidOperationException("Not sufficient funds for this withdrawal");
            }

            var withdrawal = new 交易(-amount, date, note);
            all交易s.Add(withdrawal);
        }

        public string GetAccountHistory()
        {
            var report = new System.Text.StringBuilder();

            decimal balance = 0;
            report.AppendLine("Date\t\tBalance\tNote");
            foreach(var item in all交易s)
            {
                balance += item.Amount;
                report.AppendLine($"{item.Date.ToShortDateString()}\t{balance}\t{item.Notes}");
            }
            return report.ToString();
        }

    }
}

Transaction Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classes
{
    public class 交易
    {
        public decimal Amount { get; }
        public DateTime Date { get; }
        public string Notes { get; }

        public 交易(decimal amount, DateTime date, string note)
        {
            Amount = amount;
            Date = date;
            Notes = note;
        }
    }
}

示例代码工作正常,但我有一个查询。在示例列表中<Transaction> all交易 = new 清单<Transaction>() has been created.

我想知道,不同客户进行的所有交易都将添加到一个"allTransaction"列表或每个客户都有自己的"allTransaction"清单?例如Client1.allTransaction,Client2.allTransaction .....

例如,在此示例中,我已经创建了两个客户端Client1和Client2。他们所有的交易都存储在哪里?

在程序的末尾有一个GetAccountHistory()函数,它显示客户端的历史记录。如何识别哪个客户的交易。

请指导我
谢谢
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,497
地点
弗吉尼亚州切萨皮克
编程经验
10+
Each Client has it's own all交易s. Look closely at the output of the code you presented above. Do you see the two different transactions getting mixed?

我建议回顾一下对象实例和成员变量的概念。
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,921
地点
英国
编程经验
10+
设计不好,如果你问我。也许举个例子,将方法转储到类中是合理的,这些类仅应用于其中包含的上下文数据。类通常意味着具有单一用途。
考虑将方法移至业务逻辑类或某个静态帮助器类。
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,497
地点
弗吉尼亚州切萨皮克
编程经验
10+
实际上,除了公开列表以外-最好应该是私有的,或者更糟的是只读的-这是一个很棒的面向对象设计,特别是从学术角度来看。

当您开始从事该行业并且必须处理将数据持久保存到关系数据库管理系统(RDBMS)和/或必须处理ORM(Object-Relational Mapper)的实用性时,这些问题才开始出现。或序列化为JSON或XML。如果您有一个面向对象的数据库管理系统(OODBMS),它仍然是一个很好的设计。当您必须开始使用RDBMS或必须序列化为JSON或XML时,然后将POCO(普通的旧C#对象)用作DTO(数据传输对象)似乎是大多数人所采用的方法。因此,与学术性的面向对象的方法不同,在这种方法中,对象同时拥有两个数据 行为,一些行业程序员倾向于采用某些对象具有的方法 只是 数据,而其他对象有 只是 行为(又称业务逻辑)并让后者对前者进行操作。
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,498
地点
悉尼,澳大利亚
编程经验
10+
我认为BankAccount类将分配给每个客户。对?
并且BankAccount类中的内容将复制到每个客户
That's the idea. It's pretty simple, really, but beginners tend to ignore the fact that the whole point of OOP is that programming objects are based on real-world objects 和 how they behave. Think the concept of a car. If I talk about cars to you, you know what I'm talking about, right? A metal box with an engine, four wheels, seats, etc, etc. That concept that we both understand is the class, i.e. the description of what the class is, has 和 does. That we understand what it is 和 can talk about it doesn't mean that either of us actually have one though. A manufacturer still needs to build the actual cars 和 then we need to buy them 和 take them home. If you 和 I both buy a car then they are both car objects that are based on the same template but have various differences 和 are distinct from each other. That's how OOP works. In your code, the BankAccount class is the template but each instance of that class you create - each object of that type - is different 和 distinct. A bank account is a bit more abstract than a car but, even in the real world, you wouldn't expect any overlap between them if you 和 I both went into the same branch 和 each opened a new bank account. Our money would not be mixed 和 our transaction histories would be completely separate.
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,921
地点
英国
编程经验
10+

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,497
地点
弗吉尼亚州切萨皮克
编程经验
10+
编程全部与抽象有关。  :) 何时应用正确的抽象水平与具体水平相结合的艺术?

As an aside, I looked at the original link from the OP. The 清单 all交易s was marked as private in the Microsoft code.
 
最佳 底部