有没有一种方法可以通过 c# 中的方法将 int 的值从一个对象传输到同一 class 中的另一个对象

Is there a way to transfer a value of an int from one object to another within the same class via method in c#

class Bank
{
    private String accoutNumber;
    private double credit;

    public String AccountNumber
    {
        get { return accoutNumber; }
        set { accoutNumber = value; }
    }

    public double Credit
    {
        get { return credit; }
        set { credit = value; }
    }
        
    public Bank() { }

    public Bank(String accoutNumber)
    {
        this.accoutNumber = accoutNumber;
    }

    public Bank(String accoutNumber, int credit)
    {
        this.accoutNumber = accoutNumber;
        this.credit = credit;
    }

    public void addBalance(int amount) { credit += amount; }

    public void vyber(int amount)
    {
        if (amount > credit)
            return;

        credit -= amount;
    }

    public void transfer(int amount, String accoutNumber)
    {
        if (amount > credit)
            return;

        //else transfer from one object to another
    }
}

(在另一个文件中,我使用转移方法将信用从一个对象转移到另一个对象,我只是不知道该怎么做,而且我没有使用任何数据库文件,它就像有可能)

Bank object1 = new Bank("1234567890/1234", 10000);
Bank object2 = new Bank("7845213154/1448", 7000);
object1.transfer("7845213154/1448", 2000)
//object1's credit = 8000
//object2's credit = 9000

听起来你想在这里做的是:

  • 您有多个某种类型的对象(可能称为 Account),每个对象都有一个由 accountNumber
  • 定义的标识
  • 转账时,您想按号码查找不同的帐户,然后访问that

现在:没有对象的自动预构建索引 accountNumber。这是您需要单独添加的东西。例如,您可能有一个 Dictionary<string, Account>,您将每个实例添加到:

var foo = new Account { accoutNumber = "12391", Credit = 420 };
var bar = new Account { accoutNumber = "58u98:a24", Credit = 9000 };

var accounts = new Dictionary<string, Account>();
accounts.Add(foo.accoutNumber , foo);
accounts.Add(bar.accoutNumber , bar);
// etc

现在,我们可以通过身份获取账号:

if (!accounts.TryGetValue(someAccountId, out var someAccount))
{
    throw new KeyNotFoundException("Target bank account not found");
}
// here, someAccount is the one we want by someAccountId
someAccount.DoSomething();

然而,这带来了一个问题;您通常不会期望每个 Account 对象都持有整个帐户集;你可以有一些帐户管理类型(维护字典)执行both查找并执行bothdeltas :

if (!accounts.TryGetValue(fromId, out var from))
{
    throw new KeyNotFoundException("Source bank account not found");
}
if (!accounts.TryGetValue(toId, out var to))
{
    throw new KeyNotFoundException("Destination bank account not found");
}
if (from.Balance < amount)
{
    throw new InvalidOperationException("Insufficient funds in source account");
}
from.AddBalance(-amount);
to.AddBalance(amount);

请注意,这不是线程安全的。我猜线程与您正在做的事情无关。

另一种方法是将帐户查询传递给您的方法。这可能是字典本身,或一些辅助服务:

object1.Transfer("7845213154/1448", 2000, accounts);

并让 Transfer 方法在您需要的任何验证之后在内部进行查找:

public void Transfer(decimal amount, string accountNumber, Dictionary<string, Account> accounts)
{
    if (!accounts.TryGetValue(accountNumber, out var to))
    {
        throw new KeyNotFoundException("Destination bank account not found");
    }
    if (amount > credit)
    {
        throw new InvalidOperationException("Insufficient funds in source account");
    }
    AddBalance(-amount);
    to.AddBalance(amount);
}

同样,这在任何方面都不是线程安全的,也不是事务性的等等。

会像这样(在这里和那里改进一些关于验证帐号等的规则)。

public class Bank
{
    private static global::System.Collections.Generic.Dictionary<string, global::Bank> accounts = new System.Collections.Generic.Dictionary<string, global::Bank>(10000);
    private string accountNumber;
    private decimal credit;
    public string AccountNumber { get { return this.accountNumber; } }
    public decimal Credit { get { return this.credit; } }
    public void addBalance(decimal amount) { this.credit += amount; }
    public void vyber(int amount) { if (amount <= credit) { this.credit -= amount; } }

    public static global::Bank GetAccount(string number)
    {
        if (string.IsNullOrEmpty(number)) { return null; }
        else
        {
            global::Bank bank = global::Bank.accounts[number];
            if (bank == null) { bank = new global::Bank(number); }
            return bank;
        }
    }

    public void transfer(decimal amount, string number)
    {
        if (amount <= credit && number != this.accountNumber)
        {
            global::Bank bank = global::Bank.GetAccount(number);
            if (bank == null) { throw new global::System.ArgumentException("Not Found"); }
            else
            {
                this.credit -= amount;
                bank.addBalance(amount);
            }
        }
    }

    private Bank(string number) { this.accountNumber = number; }
}