English 中文(简体)
多种形态/冲淡
原标题:Polymorphism/Overriding

是否可以解释这两个例子之间的区别?

Class A

<编码>保护虚拟定位:

Class B

如下:

Class A

Class B

http://www.un.org/Depts/DGACM/index_french.htm

Assuming that Class B inherits from Class A .

我始终认为,如果你想要凌驾于一种方法之上的话,你就必须在上等舱和<代码>上使用<代码>virtual,然而,我试图删除关键词和汇编的罚款方案。 差别究竟是什么?

最佳回答

你所举的第二个例子掩盖了母子的“GetData”,它并没有推翻它。

例:

private class Base
{
    public virtual void Test()
    {
        Console.WriteLine("Base");
    }

    public void Test2()
    {
        Console.WriteLine("Base");
    }
}

private class Derived : Base
{
    public override void Test()
    {
        Console.WriteLine("Derived");
    }

    public void Test2()
    {
        Console.WriteLine("Derived");
    }
}

static void Main()
{
    Base b = new Base();
    Derived d = new Derived();
    Base dInB = new Derived();

    b.Test();
    d.Test();
    dInB.Test();

    b.Test2();
    d.Test2();
    dInB.Test2();

    Console.ReadKey(true);
}

产出:

Base    // Base.Test()
Derived // Derived.Test()
Derived // Derived.Test()
Base    // Base.Test2()
Derived // Derived.Test2()
Base    // You think you re calling Derived.Test2(), but you actually call Base.Test2()

实际上,该样本无效,因为它应当使用<代码>new,关键词载于public new void Test2( in the Derived category。

它与运营商超负荷运作一样。 这实际上并不凌驾于任何东西之上。 当您有准确的类型<代码> 衍生出时,则采用新的方法。

www.un.org/Depts/DGACM/index_spanish.htm 你们必须真正谨慎地与隐蔽的成员打交道,这根本不像压倒一切(阶级)或执行(内部)。 只有当您具备<><>>>>>>> >> > > >。 字母缩略语 method,否则就仍然指基型方法!

问题回答

不同之处在于,在第一个案例中,你是压倒一切的,在第二种情况下,你藏匿起来完全不同。

在第一个案件中:

class B: A
{
    void Foo()
    {
        B b = new B();
        A a = b;

        a.GetData() //B s GetData() will be called
        b.GetData() //B s GetData() will be called
    }
}

另一方面,第二起案件:

class B: A
{
    void Foo()
    {
        B b = new B();
        A a = b;

        a.GetData() //A s GetData() will be called
        b.GetData() //B s GetData() will be called
    }
}

在第二种情况下,你只是躲藏了GetData(GetData)(A)的实施,但是,即使变数指的是B类,你总是能够通过一个变式的A来指执行。

public class A
{
    public virtual string GetData() { return "A";}
}

public class B : A
{
    public override string GetData() { return "B"; }
}

如果你在使用下面的24个班级,你会有什么期望?

        A a = new A();
        B b = new B();
        A c = new B();

        Console.WriteLine(a.GetData());
        Console.WriteLine(b.GetData());
        Console.WriteLine(c.GetData());

这将印刷“A”“B”“B”。 变数c被储存为A类,但在实施该方法时,该代码被“实际”执行。 (见虚拟职能表和解决原则的角度)

如果你不像以下代码那样使用虚拟和压倒性,将印刷“A”“B”“A”。

public class A
{
    public string GetData() { return "A";}
}

public class B : A
{
    public new string GetData() { return "B"; }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签