请解释隐藏在c#中的方法隐藏的概念 [英] please explain the concept of method hiding in c#

查看:59
本文介绍了请解释隐藏在c#中的方法隐藏的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请任何人解释隐藏在c#中的方法的概念,例如,我没有清楚地说明...提前感谢....

please anyone explain the concept of method hiding in c# with example,i am not getting it clearly...thanks in advance....

推荐答案

方法隐藏在该术语的最一般意义上可以被认为是封装的现代编程策略的一部分,并使用继承来扩展,覆盖,修改方法名称的含义 ...当发生时会发生什么你在程序的某一点执行一个具有给定名称的方法。



在C#中,当一个Class继承自的时候,方法隐藏以各种方式实现另一个类:从继承的类通常被称为'基类,从'基类继承的类通常被称为'派生类。



1.'new修饰符可用于方法,以指示该方法隐藏了另一个方法,该方法具有在继承/派生类的父/基类中定义的相同名称。



您可以使用声明为'new的方法来扩展基类方法的功能,这意味着:您可以执行其他操作,然后使用基类方法,另外,只需使用特殊引用'base。'当你应该使用'new时,.NET编译器将警告你,但是允许你在不使用的情况下运行你的应用程序它。



2.'override修饰符可用于使派生类中的方法隐藏基类中具有相同名称的方法,但仅限于该方法以某种特殊方式(抽象,虚拟)在基类中声明:参见:[ ^ ]。



这里有一些类定义:
"Method Hiding" in the most general sense of that term can be considered part of the modern programming strategies of encapsulation and using inheritance to extend, override, modify the meaning of a method name ... what happens when you execute a method with a given name at a certain point in your program.

In C# "method hiding" is implemented in a variety of ways when one Class inherits from another Class: the class inherited from is usually referred to as the 'base class, the classes that inherit from the 'Base Class are usually referred to as 'derived classes.

1. the 'new modifier can be used on a method to indicate that the method hides another method with the same name that is defined in the parent/base Class of the inheriting/derived Class.

You can use methods declared as 'new to extend the functionality of a base class method, meaning: you can perform additional operations, and then use the base class method, also, by just using the special reference 'base.' The .NET compiler will warn you when you should use 'new, but will allow you to run your application without using it.

2. the 'override modifier can be used to make a method in a derived class hide the method with the same name in the base class, but only if that method is declared in the base class in certain special way (abstract, virtual): see: [^].

Here's some class definitions:
public class baseNewAndOverRide
{
    public string getString(string arg)
    {
        return "in base class getString: " + arg;
    }

    public virtual string getString2(string arg)
    {
        return "in base class getString2: " + arg;
    }
}

public class child1 : baseNewAndOverRide
{
    public string getString(string arg)
    {
        return "in child1 class: " + arg;
    }

    public string getStringFromBase(string arg)
    {
        return base.getString(arg);
    }
}

public class child2 : baseNewAndOverRide
{
    public new string getString(string arg)
    {
        return "in child2 class: " + arg;
    }

    public string getStringFromBase(string arg)
    {
        return base.getString(arg);
    }
}

public class childOfChild1 : child1
{
    public new string getString(string arg)
    {
        return "in childOfChild1 class: " + arg;
    }

    public string getStringFromBase(string arg)
    {
        return base.getString(arg);
    }
}

public class childOfChild2 : child2
{
    public override string getString2(string arg)
    {
        return "in childOfChild2 class: " + arg;
    }

    public string getString2FromBase(string arg)
    {
        return base.getString2(arg);
    }
}

下面是一些尝试使用这些定义的代码:在此代码的第一行放置一个断点,并使用F11单步执行它,看看会发生什么

Here's some code to try out with these definitions: Put a break-point at the the first line in this code, and use F11 to single-step through it, watching what happens

public void testNewOverride()
{
    // put a break-point here !
    child1 c1 = new child1();

    string child1Test = c1.getString("hello");

    string baseFromChild1Test = c1.getStringFromBase("hello");

    child2 c2 = new child2();

    string child2Test = c2.getString("hello");

    string baseFromChild2Test = c2.getStringFromBase("hello");

    childOfChild1 c3 = new childOfChild1();

    string child3Test = c3.getString("hello");

    string baseFromChild3Test = c3.getStringFromBase("hello");

    childOfChild2 c4 = new childOfChild2();

    string child4Test = c4.getString2("hello");

    string child4Test2 = c4.getString("hello");

    string baseFromChild4Test = c4.getString2FromBase("hello");
}


隐藏在C#中的方法 [ ^ ]


这篇关于请解释隐藏在c#中的方法隐藏的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆