以下是完整代码(如果您不需要,请跳过它):
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.
答案 0 :(得分:2)
Invitation
方法在if条件中调用WorkOffer
方法。
这意味着DDEL
直接调用WorkOffer
,也间接调用Invitation
。
考虑到这一点,您不需要直接通过代理人致电WorkOffer
答案 1 :(得分:0)
答案很简单:
您正在拨打WorkOffer
两次。一旦进入代表,一次进入Invitation
。