成员“methodhiding.parttimeemployee.printfullname()”不会隐藏继承的成员

时间:2015-03-19 17:54:25

标签: c# oop visual-studio-2012 monodevelop

我无法弄清楚问题。该程序应该隐藏员工类中的'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();

        }
    }
}

2 个答案:

答案 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 ");
    }
}