C#使用委托(当前上下文中不存在该名称)

时间:2018-03-27 19:59:41

标签: c# methods delegates

我有以下代码:

using System;
namespace Laboratory_work
{

    public delegate void Delegate ();
    public interface Method1
    {
        string InformationOutput (Person obj);
        int WorkOffer();
    }
    public interface Method2
    {
        int Invitation();
    }

    public abstract class Person: Method1
    {
        public string Name{get; set;}
        public string Surname{get; set;}
        public int Age{get; set;}
        public virtual string InformationOutput(Person obj)
        {
            string info="Some personal information \n_________________________\n\n"+
                "Full name — "+Name+" "+Surname+
                "\nAge — "+Age+" years\n";
            return info;
        }
        public int WorkOffer()
        {
            if (Age>=18)
            {
                Console.WriteLine("\nYou may be offered a job.");
                return 1;
            }
            else
            {
                Console.WriteLine("\nUnfortunately, there is no job for you.");
                return 0;
            }
        }
    }
    public class WhiteCollarWorker: Person, Method1
    {
        public string Occupation{get; set;}
        public bool ComputerAvailable{get; set;}
        public override string InformationOutput(Person obj)
        {
            return base.InformationOutput(obj)+"Occupation — "+Occupation+"\n"+
                "Computer — "+ComputerAvailable+"\n";
        }
        public int WorkOffer()
        {
            if (Age>=21 && Occupation!="\0")
            {
                Console.WriteLine("\nYou may be offered a job.");
                return 1;
            }
            else
            {
                Console.WriteLine("\nUnfortunately, there is no job for you.");
                return 0;
            }
        }
    }
    public class Laborer: Person, Method1
    {
        public string Occupation{get; set;}
        public bool InstrumentsAvailable{get; set;}
        public override string InformationOutput(Person obj)
        {
            return base.InformationOutput(obj)+"Occupation — "+Occupation+"\n"+
                "Instruments — "+InstrumentsAvailable+"\n";
        }
        public int WorkOffer()
        {
            if (Age>=18 && Occupation!="\0")
            {
                Console.WriteLine("\nYou may be offered a job.");
                return 1;
            }
            else
            {
                Console.WriteLine("\nUnfortunately, there is no job for you.");
                return 0;
            }
        }
    }
    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>=25 && 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;  
            }
        }
    }

    public class Builder: Laborer, Method1, Method2
    {
        public int YearExperience {get; set;}
        public bool Education {get; set;}
        public override string InformationOutput(Person obj)
        {
            return base.InformationOutput(obj)+"Experience — "+YearExperience+" year(s)\n"+
                "Education — "+Education+"\n";
        }
        public int WorkOffer()
        {
            if (Age>=18 && (YearExperience>=1 || Education==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;  
            }
        }


    }
    class Program
    {   
    public static void Main()
    {
        Delegate Del_1=new Delegate(WorkOffer);
        Delegate Del_2=new Delegate(Invitation);
        Delegate TwoDels=new Delegate(Del_1+Del_2);

        const int N=10;
        int Count=0;

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

            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());

            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++)
        {
            Console.WriteLine("\n\n#"+(i+1));
            Console.WriteLine(persons1[i].InformationOutput(persons1[i]));
        }
        Count=0;
        var persons2=new Builder[N];
        for (int i=0; i<10; i++)
        {
            persons2[i] = new Builder();

            Console.WriteLine("\n\nBUILDERS\n________\n");
            Console.WriteLine("Input information of a person");
            Console.Write("Name: ");
            persons2[i].Name=Console.ReadLine();
            Console.Write("Surname: ");
            persons2[i].Surname=Console.ReadLine();
            Console.Write("Age: ");
            persons2[i].Age=int.Parse(Console.ReadLine());
            Console.Write("Occupation: ");
            persons2[i].Occupation=Console.ReadLine();
            Console.Write("Instruments (true or false): ");
            persons2[i].InstrumentsAvailable=bool.Parse(Console.ReadLine());
            Console.Write("Experience (in years): ");
            persons2[i].YearExperience=int.Parse(Console.ReadLine());
            Console.Write("Education (true or false): ");
            persons2[i].Education=bool.Parse(Console.ReadLine());

            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++)
        {
            Console.WriteLine("\n\n#"+(i+1));
            Console.WriteLine(persons2[i].InformationOutput(persons2[i]));
        }
        Console.WriteLine("\nPress any key to continue . . .");
        Console.ReadKey(true);
    }
    }
}

它很松散,所以我想把注意力集中在第176-178行:

Delegate Del_1=new Delegate(WorkOffer);
Delegate Del_2=new Delegate(Invitation);
Delegate TwoDels=new Delegate(Del_1+Del_2);

在这里,我正在尝试创建Delegate的示例,导致出错。编译器显示错误“当前上下文中不存在名称'WorkOffer'”和“当前上下文中不存在名称'邀请'”。 我认为这都是因为我尝试在声明这些方法的类中使用它们,但是我应该怎样做才能在我的程序中使用连接到这些方法的委托?我需要一些建议。

0 个答案:

没有答案