我无法弄清楚问题。该程序应该隐藏员工类中的'Printfullname()'。
using System;
namespace MethodHiding
{
public class Employee
{
public string first;
public string last;
public void PrintFullName()
{
Console.WriteLine(first + " " + last);
}
}
public class PartTimeEmployee : Employee
{
public new void PrintFullname()
{
Console.WriteLine(first + " " + last + "- Contractor ");
}
}
public class FullTimeEmployee : Employee
{
}
class MainClass
{
public static void Main (string[] args)
{
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.first = "abc";
PTE.last = "Sarfraz";
PTE.PrintFullName();
FullTimeEmployee FTE = new FullTimeEmployee();
FTE.first= "hjk";
FTE.last = "poi";
FTE.PrintFullName();
}
}
}
答案 0 :(得分:2)
public virtual void PrintFullName()
{
Console.WriteLine(first + " " + last);
}
public override void PrintFullName()
{
Console.WriteLine(first + " " + last + "- Contractor ");
}
除非你必须,否则不要使用新的。同样,Eugene Podskal指出c#是区分大小写的。
答案 1 :(得分:0)
PrintFullname 不存在。也许你的意思是 PrintFullName ?
public class PartTimeEmployee : Employee
{
public new void PrintFullname() < this should be PrintFullName
{
Console.WriteLine(first + " " + last + "- Contractor ");
}
}