Rewrite, overloading and hiding

Rewrite, overloading and hidden knowledge is often used in development, and today we are here speaking about the difference between the three of them.

Zero, rewrite

The so-called rewrite, is the inheritance, the subclass to redefine the parent class method, there should be noted that:

  1. It must be overridden in the class inheritance relations;
  2. Subclass overrides the method name and parameter list must match the method name and parameter list of the parent class;
  3. Methods parent virtual modification;
  4. Methods Subclasses override modification;
  5. Method override interface and for rewriting the general class inheritance;
  6. Regardless of the method to access the parent class or sub-class, sub-class method is invoked.

We look through the code rewrite of view:

  1. Create a parent class Cat
public class Cat
{
    public virtual void Call()
    {
        Console.WriteLine("小猫喵喵叫");
    }
}
  1. Creating a subclass BlackCat
public class BlackCat:Cat
{
    public override void Call()
    {
        Console.WriteLine("黑猫喵喵叫!");
    }
}
  1. transfer
class Program
{
    static void Main(string[] args)
    {
        Cat cat = new BlackCat();
        //输出 “黑猫喵喵叫!”
        cat.Call();

        BlackCat blackCat = new BlackCat();
        //输出 “黑猫喵喵叫!”
        blackCat.Call();

        Console.Read();
    }
}

A heavy-duty

The so-called heavy load is in the same scope, there is the same but different parameter lists multiple names of methods to determine which specific method calls by wearing a different point of argument. There is one caveat: The return value can not be called different overloaded. Similarly, we look through the code to reload:

  1. However, different parameters defined in the same way three names
public class Person
{
    public void Info()
    {
        Console.WriteLine("我是小明");
    }

    public void Info(string sex)
    {
        Console.WriteLine("我是小明,性别" + sex);
    }

    public void Info(string name, int age, string sex)
    {
        Console.WriteLine("我是" + name + ",今年" + age + "岁,性别" + sex);
    }
}
  1. transfer
class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();

        //输出“我是小明”
        person.Info();

        //输出 “我是小明,性别男”
        person.Info("男");

        //输出 “我是小红,今年10岁,性别女”
        person.Info("小红", 10, "女");

        Console.Read();
    }
}

Second, hide

Hidden is more interesting, also known as cover to hide the parent class method, without notice, the subclass method by new hidden keywords speaking methods, but he will not change the parent class method, that is to say: access parent, call the parent class methods, visit the subclass, the subclass method calls. This difference with the rewrite. We use hidden when the need to note the following:

  1. Logo hidden method must exactly match the flag is hidden methods;
  2. The return value of the method must return hidden and hidden same method;
  3. Abnormal abnormality consistent hiding method must be thrown thrown hiding method and the, or is a subclass;
  4. Hidden method can not be private, otherwise it is just a new subclass defines a method, and not be hidden.

Similarly, we look through the code:

  1. The definition of the parent class
public class Cat
{
    public void Call()
    {
        Console.WriteLine("小猫喵喵叫!");
    }
}
  1. Defined subclass
public class BlackCat : Cat
{
    new public void Call()
    {
        Console.WriteLine("黑猫喵喵叫!");
    }
}
  1. transfer
class Program
{
    static void Main(string[] args)
    {
        Cat cat = new BlackCat();
        //输出 “小猫喵喵叫!”
        cat.Call();

        BlackCat blackCat = new BlackCat();

        //输出 “黑猫喵喵叫!”
        blackCat.Call();

        Console.Read();
    }
}

Note: Hidden mainly used in the case can not change the parent class method

Third, the summary

According to the above summarized below to explain the difference between:

  1. Overloading is the same as the method name, parameters (the number / type) different;
  2. Rewriting method is to redefine the parent class, you need to use the virtual and the override ;
  3. Hiding parent does not change.

Guess you like

Origin www.cnblogs.com/gangzhucoll/p/11260894.html