【问题标题】:C# Why method performs two times?C# 为什么方法执行两次?
【发布时间】:2018-03-29 18:38:48
【问题描述】:

这是完整的代码(如果不需要,请跳过它):

using System;
namespace Laboratory_work
{
    class Program
    {   
    //some code
    public class Engineer: WhiteCollarWorker, Method1, Method2
    {
        public int YearExperience{get; set;}
        public bool BachelorDegree{get; set;}
        public bool MasterDegree{get; set;}
        public override string InformationOutput(Person obj)
        {
            return base.InformationOutput(obj)+"Experience — "+YearExperience+" year(s)\n"+
                "Bachelor Degree — "+BachelorDegree+"\n"+
                "Master Degree — "+MasterDegree+"\n";
        }
        public int WorkOffer()
        {
            if (Age>=21 && YearExperience>=1 && BachelorDegree==true)
            {
                Console.WriteLine("\nYou may be offered a job.");
                return 1;
            }
            else
            {
                Console.WriteLine("\nUnfortunately, there is no job for you.");
                return 0;
            }
        }
        public int Invitation()
        {
            if(WorkOffer()==1)
            {
                Console.WriteLine("Dear "+Name+" "+Surname+", soon you will receive\n"+
                                  "an email with date and time of your interview\n"+
                                  "at out company.Thank you for your CV.");
                return 1;                  
            }
            else
            {
                Console.WriteLine("Dear "+Name+" "+Surname+", unfortunately, you are\n"+
                                  "not suitable for this position. Than you for your CV.\n");
                return 0;  
            }
        }
    }

    //some code

    public static void Main()
    {
        const int N=10;
        int Count=0;

        var persons1=new Engineer[N];
        for (int i=0; i<10; i++)
        {
            persons1[i] = new Engineer();

            Delegate DELnum1=persons1[i].WorkOffer;
            Delegate DELnum2=persons1[i].Invitation;
            Delegate DDEL=DELnum1+DELnum2;

            Console.WriteLine("\n\nENGINEERS\n________\n");
            Console.WriteLine("Input information of a person");
            Console.Write("Name: ");
            persons1[i].Name=Console.ReadLine();
            Console.Write("Surname: ");
            persons1[i].Surname=Console.ReadLine();
            Console.Write("Age: ");
            persons1[i].Age=int.Parse(Console.ReadLine());
            Console.Write("Occupation: ");
            persons1[i].Occupation=Console.ReadLine();
            Console.Write("Computer (true or false): ");
            persons1[i].ComputerAvailable=bool.Parse(Console.ReadLine());
            Console.Write("Experience (in years): ");
            persons1[i].YearExperience=int.Parse(Console.ReadLine());
            Console.Write("Bachelor Degree (true or false): ");
            persons1[i].BachelorDegree=bool.Parse(Console.ReadLine());
            Console.Write("Master Degree (true or false): ");
            persons1[i].MasterDegree=bool.Parse(Console.ReadLine());

            DDEL(); 

            Count++;
            Console.WriteLine("Do you want to continue? (Print 'OK' or 'NO'.)");
            string answer;
            answer=Console.ReadLine();
            if (answer=="OK"||answer=="ok") continue;
            else break;
        }
        for(int i=0; i<Count;i++)
        {
            Delegate DELnum2=persons1[i].Invitation;
            Console.WriteLine("\n\n#"+(i+1));
            Console.WriteLine(persons1[i].InformationOutput(persons1[i]));
            if (DELnum2()==1) Console.WriteLine("\nAccepted for work.");
            else Console.WriteLine("\nNot accepted for work.");
        }
        //some code
        Console.WriteLine("\nPress any key to continue . . .");
        Console.ReadKey(true);
    }
    }
}

我创建了委托实例并使用它们来执行两种方法。 代表:

public delegate int Delegate ();

方法(工程师类):

public int WorkOffer()
        {
            if (Age>=21 && YearExperience>=1 && BachelorDegree==true)
            {
                Console.WriteLine("\nYou may be offered a job.");
                return 1;
            }
            else
            {
                Console.WriteLine("\nUnfortunately, there is no job for you.");
                return 0;
            }
        }
        public int Invitation()
        {
            if(WorkOffer()==1)
            {
                Console.WriteLine("Dear "+Name+" "+Surname+", soon you will receive\n"+
                                  "an email with date and time of your interview\n"+
                                  "at out company.Thank you for your CV.");
                return 1;                  
            }
            else
            {
                Console.WriteLine("Dear "+Name+" "+Surname+", unfortunately, you are\n"+
                                  "not suitable for this position. Than you for your CV.\n");
                return 0;  
            }
        }

这就是我在一个委托中连接这些方法的方式(循环):

    Delegate DELnum1=persons1[i].WorkOffer;
    Delegate DELnum2=persons1[i].Invitation;
    Delegate DDEL=DELnum1+DELnum2;

然后使用它们:

DDEL(); 

但在输出中我有两个消息“不幸的是,你没有工作”。为什么?谁能解释一下? Output. Excuse me for random input.

【问题讨论】:

标签: c# methods delegates


【解决方案1】:

Invitation 方法在 if 条件下调用WorkOffer 方法。

这意味着DDEL 直接调用WorkOffer,也间接通过Invitation

考虑到这一点,您无需直接通过代理调用WorkOffer

【讨论】:

    【解决方案2】:

    答案很简单:
    你打电话给WorkOffer 两次。一次在代理中,一次在Invitation

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多