具有阴影和覆盖的继承 [英] Inheritance with Shadowing and Overriding

查看:102
本文介绍了具有阴影和覆盖的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,



我知道互联网上有很多关于继承,覆盖和阴影的信息。它们之间的区别等等。



有一些现实生活场景的例子也很好地解释了。阅读其中一些我可以说: -

影子为现有物品赋予了新生命。

压倒正在改变现有生活。



为了清楚地理解它,我写了一个简单的测试程序。

Hello Friends,

I know there are many information available on internet for Inheritance, Overriding and Shadowing. The difference between them etc. etc.

There are some real life scenario with example also explained very well. After reading some of those i can say :-
Shadowing is giving a new life to existing items.
Overriding is changing existing life.

To understand it clearly i wrote a simple test program.

public class cls1
    {
        public void TestFoo() { Console.Write("Base Class TestFoo. \n"); }
        public virtual void fun1()
        {
            Console.Write("cls1 fun1 \n");
        }
        public void foo() { Console.Write("Base class function.\n"); }
    }

    public class cls2 : cls1
    {
        public void TestFoo() { Console.Write("Child Class TestFoo. \n"); }
        public override void fun1()
        {
            Console.Write("cls2 fun1 \n");
        }
        public new void foo() { Console.Write("Child class function.\n"); }
    }

   static void Main(string[] args)
        {
            // Normal

            cls1 objTestFoo = new cls1();
            objTestFoo.TestFoo(); // "Base class TestFoo"

            cls1 objTestFoo1 = new cls2();
            objTestFoo1.TestFoo(); // "Base class TestFoo" ???? Why it is calling Base class function
 
            cls2 objTestFoo2 = new cls2();
            objTestFoo2.TestFoo(); // "Child class TestFoo"           

            // overide

            cls1 obj1 = new cls1();
            obj1.fun1(); // "cls1 fun1"

            cls1 obj2 = new cls2();
            obj2.fun1(); // "cls2 fun1" ????? why it is calling child class fun1

            cls2 obj3 = new cls2();
            obj3.fun1(); // "cls2 fun1"
            
            // new 

            cls1 obj1_new = new cls1();
            obj1_new.foo(); // "Base Class function"

            cls1 obj2_new = new cls2(); ???? why it is calling base class function
            obj2_new.foo(); // "Base Class function"

            cls2 obj3_new = new cls2();
            obj3_new.foo(); // "Child Class function"
}



我无法理解的是当我们在子类对象中引用基类时调用不同的函数。


What i am not able to understand is why it is calling different functions when we give reference of base class in to child class object.

推荐答案

它是只是为了说明。

它没有真正起作用的例子就是你有b $ b
It's just for illustration.
It doesn't really work as an example is you have
BaseClass bc = new BaseClass();
bc.MyMethod();
DerivedClass dc = new DerivedClass();
dc.MyMethod();

给予:

Giving:

BaseClass MyMethod called
DerivedClass MyMethod called

因为你可以在没有得到任何类的情况下得到那个结果!

如果你把上面的内容写成

Because you could get that result without deriving any class at all!
If you write the above as

BaseClass example = new BaseClass();
example.MyMethod();
example = new DerivedClass();
example.MyMethod();

并且它给出了相同的结果(这样做),表明继承有效(派生类实例引用可以分配给基类变量)和系统通过为实际实例调用适当的方法而不参考变量类型来为您排序覆盖。

当您对 new 它查看变量类型的方法:

And it gives the same result (which is does) that that shows that inheritance works (a derived class instance reference can be assigned to a base class variable) and that the system sorts out the overriding for you by calling the appropriate method for the actual instance without referring to the variable type.
When you do the same with new methods it does look at the variable type:

Base bc = new Base();
Derived dc = new Derived();
bc.MyMethod();
dc.MyMethod();
bc = dc;
bc.MyMethod();

你得到:

And you get:

Base: MyMethod called
Derived: MyMethod called
Base: MyMethod called

显示它。


这篇关于具有阴影和覆盖的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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