【发布时间】: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.
【问题讨论】:
-
您的代码必须这么长吗?创建一个最小的例子。见How to create a Minimal, Complete, and Verifiable example
-
请阅读stackoverflow.com/help/how-to-ask 并尝试将您的示例简化为仅与您遇到的问题相关的部分。 (在 90+% 的情况下,甚至无需询问就可以为您提供所需的答案)。