跪求大佬帮忙修改,请问一下转账时objA.transfer为什么不能赋值给objB.count?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{

class Depositcard                             //储蓄卡
{
    public int balance=10000;      //余额
    public int count;

    public void Repayment(int count1)
    {
        count1=balance - count;
        if (count1<0)
        {
            Console.WriteLine("转账失败,储蓄卡还剩:" + balance);
        }
        else
        {
            Console.WriteLine("转账成功,储蓄卡还剩:" + balance);
        }
    }
}

class Creditcard                    //信用卡
{

    public int sum;                 //余额
    public int saveordraw;          //存钱或取钱
    public int transfer;            //转账
    public int balance;             //储蓄卡转账
    public Creditcard(int num)
    {
        sum = num;
        Console.WriteLine("信用卡还有余额" + sum, "元");
    }

    public void Save_money()        //存钱
    {
        string a = Console.ReadLine();
        saveordraw = int.Parse(a);

        sum += saveordraw;
        Console.WriteLine("信用卡还有余额" + sum, "元");
    }

    public void Draw_money()       //取钱
    {
        string b = Console.ReadLine();
        saveordraw = int.Parse(b);          
        sum -= saveordraw;
        if(sum<0)
            Console.WriteLine("余额不够,取款失败!");
        else
            Console.WriteLine("取款成功,信用卡还有余额" + sum, "元");
    }

    public void Transfer_money()
    {
        string c = Console.ReadLine();
        transfer = int.Parse(c);

        Console.WriteLine(transfer);
        if (balance<transfer)
            Console.WriteLine("储蓄卡余额不够,转账失败!");
        else
        {
            sum += transfer;
            Console.WriteLine("转账成功,信用卡还有余额" + sum, "元");

        }
    }


    public string count3;
    public void Show()
    {
        Console.WriteLine("1.存款");
        Console.WriteLine("2.取款");
        Console.WriteLine("3.转账");
        Console.WriteLine("请输入需要的操作");
    }
    public void Choose_methods(int a)
    {

        switch (count3)
        {
            case "1":
                Console.WriteLine("请输入存款金额");
                Save_money();                  
                break;
            case "2":
                Console.WriteLine("请输入取款金额");
                Draw_money();
                break;

            case "3":
                Console.WriteLine("请输入转账金额");
                Transfer_money();
                break;

        }
    }
}

class Delegate
{
     public delegate void Mydelegate(int num);
     public event Mydelegate Repayment;
     public void Events()
     {
           int money = 0;

           Repayment(money);

     }
}

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            int sum = -50000;         //此处为欠款
            Creditcard objA = new Creditcard(sum);
            Depositcard objB = new Depositcard();
            Delegate objC = new Delegate();                
            objA.balance = objB.balance;  
            objA.Show();
            objA.count3 = Console.ReadLine(); 


            objC.Repayment += new Delegate.Mydelegate(objA.Choose_methods);
               objB.count = objA.transfer; 

            if (objA.count3 == "3")
            {                                           
               objC.Repayment += new Delegate.Mydelegate(objB.Repayment);
            }
            Console.WriteLine(objB.count);
            //Console.WriteLine(objA.transfer);
           //Console.WriteLine(objA.balance);
            objC.Events();
            Console.ReadLine();



        }

    }

}

2个回答

不是很懂这个

``
class Depositcard //储蓄卡
{
public int balance=10000; //余额
public int count;

public void Repayment(int count1)
{
    count1=balance - count;           //**你传入count1,然后用 count1=balance - count; ???不应该是count=balance - count1; ?**
    if (count1<0)
    {
        Console.WriteLine("转账失败,储蓄卡还剩:" + balance);
    }
    else
    {
        Console.WriteLine("转账成功,储蓄卡还剩:" + balance);
    }
}

}`


caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复: 谢谢~
一年多之前 回复
qq_23126581
渊渟无迹静涛君 回复: 国庆节快乐哦。回家了。
一年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复: 没
一年多之前 回复
qq_23126581
渊渟无迹静涛君 回复: 哈哈,大佬还没睡?
一年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 看了半天又是委托又是一堆类,然后还翻车了。懒得看了。
一年多之前 回复

main方法中的
objC.Repayment += new Delegate.Mydelegate(objA.Choose_methods);
只是将objA.Choose_methods的代理放入objC.Repayment的调用队列,然而并未调用,只有当objC.Events();执行的时候,代理方法才会被调用,因此
objB.count = objA.transfer会在输入转账金额以前被执行,因此objB.count无法获取到实际的 objA.transfer
如果你的问题是为什么objB.count = objA.transfer语句执行以后,当objA.transfer变化时,objB.count没有变化这个意思,那么这个问题就在于
objB.count = objA.transfer;这是一个赋值语句,因为都是int类型的数据,是值拷贝,如果希望做到联动变化,可以将transfer和count都指向同一个类的实例。
另外Depositcard.Repayment的count1参数没有看出来是什么作用?大概改了一下,你看一下是不是你要的结果
class Depositcard //储蓄卡
{
public int balance = 10000; //余额
public account count;

        public void Repayment(int count1)
        {
            //////count1什么用?
            //count1 = balance - count.price;
            balance = balance - count.price;
            if (count1 < 0)
            {
                Console.WriteLine("转账失败,储蓄卡还剩:" + balance);
            }
            else
            {
                Console.WriteLine("转账成功,储蓄卡还剩:" + balance);
            }
        }
    }

    class Creditcard                    //信用卡
    {

        public int sum;                 //余额
        public int saveordraw;          //存钱或取钱
        public account transfer;            //转账
        public int balance;             //储蓄卡转账
        public Creditcard(int num)
        {
            sum = num;
            Console.WriteLine("信用卡还有余额" + sum, "元");
        }

        public void Save_money()        //存钱
        {
            string a = Console.ReadLine();
            saveordraw = int.Parse(a);

            sum += saveordraw;
            Console.WriteLine("信用卡还有余额" + sum, "元");
        }

        public void Draw_money()       //取钱
        {
            string b = Console.ReadLine();
            saveordraw = int.Parse(b);
            sum -= saveordraw;
            if (sum < 0)
                Console.WriteLine("余额不够,取款失败!");
            else
                Console.WriteLine("取款成功,信用卡还有余额" + sum, "元");
        }

        public void Transfer_money()
        {
            string c = Console.ReadLine();
            transfer.price = int.Parse(c);

            Console.WriteLine(transfer);
            if (balance < transfer.price)
                Console.WriteLine("储蓄卡余额不够,转账失败!");
            else
            {
                sum += transfer.price;
                Console.WriteLine("转账成功,信用卡还有余额" + sum, "元");

            }
        }


        public string count3;
        public void Show()
        {
            Console.WriteLine("1.存款");
            Console.WriteLine("2.取款");
            Console.WriteLine("3.转账");
            Console.WriteLine("请输入需要的操作");
        }
        public void Choose_methods(int a)
        {

            switch (count3)
            {
                case "1":
                    Console.WriteLine("请输入存款金额");
                    Save_money();
                    break;
                case "2":
                    Console.WriteLine("请输入取款金额");
                    Draw_money();
                    break;

                case "3":
                    Console.WriteLine("请输入转账金额");
                    Transfer_money();
                    break;

            }
        }
    }

    class Delegate
    {
        public delegate void Mydelegate(int num);
        public event Mydelegate Repayment;
        public void Events()
        {
            int money = 0;

            Repayment(money);

        }
    }



    class account
    {
        public int price;
    }



    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            int sum = -50000;         //此处为欠款
            Creditcard objA = new Creditcard(sum);
            //////实例化一个类的对象
            objA.transfer = new account();
            Depositcard objB = new Depositcard();
            Delegate objC = new Delegate();
            objA.balance = objB.balance;
            objA.Show();
            objA.count3 = Console.ReadLine();

            objC.Repayment += new Delegate.Mydelegate(objA.Choose_methods);
            //////将objB的字段和objA的字段指向同一个对象
            objB.count = objA.transfer;

            if (objA.count3 == "3")
            {
                objC.Repayment += new Delegate.Mydelegate(objB.Repayment);
            }
            Console.WriteLine(objB.count);
            //Console.WriteLine(objA.transfer);
            //Console.WriteLine(objA.balance);
            objC.Events();
            Console.ReadLine();



        }

    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问