类方法隐藏故意警告

时间:2015-02-26 04:41:26

标签: c# asp.net

当我使用以下代码时,它不会给我交战。 。 。

 class Employee
{
    public string FName;
    public string LName;

    public void Print()
    {
        Console.WriteLine(FName + " " + LName);
    }
}

class FullTimeEmployee : Employee
{
    public void Print()
    {
        Console.WriteLine(FName + " " + LName + " - From FullTimeEmployee Class . . .");
    }
}

class PartTimeEmployee : Employee
{

}

class Program
{
    static void Main()
    {
        FullTimeEmployee FTE = new FullTimeEmployee();
        FTE.FName = "FullTime";
        FTE.LName = "Employee";
        FTE.Print();

        PartTimeEmployee PTE = new PartTimeEmployee();
        PTE.FName = "PartTime";
        PTE.LName = "Employee";
        PTE.Print();

        Console.Read();
    }
}

但是当我将员工课程公之于众时,它表明......警告' FullTimeEmployee.Print()'隐藏继承的成员' Employee.Print()'。如果要隐藏,请使用new关键字。 为什么会这样...... ???

1 个答案:

答案 0 :(得分:-1)

使用虚拟和覆盖。您可以通过这种方式使用子类方法覆盖父类方法。或者您可以在子类方法之前使用new运算符隐藏它

 class Employee
{
public string FName;
public string LName;

public virtual  void Print()
{
    Console.WriteLine(FName + " " + LName);
}
}

class FullTimeEmployee : Employee
{
    public override  void Print()
     {
    Console.WriteLine(FName + " " + LName + " - From FullTimeEmployee Class . . .");
   }
}

class PartTimeEmployee : Employee
{

}

class Program
{
static void Main()
{
    FullTimeEmployee FTE = new FullTimeEmployee();
    FTE.FName = "FullTime";
    FTE.LName = "Employee";
    FTE.Print();

    PartTimeEmployee PTE = new PartTimeEmployee();
    PTE.FName = "PartTime";
    PTE.LName = "Employee";
    PTE.Print();

    Console.Read();
}
}