简体   繁体   English

动态类型和静态类型C#

[英]Dynamic type and static type C#

I'm experimenting with C# and I built the following program (see below). 我正在试验C#,并构建了以下程序(请参见下文)。

I understand that the dynamic and static type of first is C . 我知道first的动态和静态类型是C For second the dynamic type is also C but the static type is A . second ,动态类型也是C而静态类型是A Now I wonder where this might come in handy? 现在,我想知道这可能会派上用场吗?

I noticed as well (obviously) that visual studio doesn't allow me to call second.CallA() . 我也很明显地注意到Visual Studio不允许我调用second.CallA()

Now notice that when I call DoA() on all three types of static type, the dynamic type is C . 现在注意,当我在所有三种静态类型上调用DoA()时,动态类型是C As this is the case, why doesn't this point to that class? 由于是这样的话,为什么不this点这门课吗? If i recall correct (I might be mistaking) in Java a self.methodA() would start looking up the inhertence tree from the caller instance. 如果我记得在Java中正确(我可能会误会),则self.methodA()将开始从调用者实例中查找继承树。 As it doesn't appear to be like that here. 因为这里看起来不像那样。 Can I create such behavior or is this a limitation of the language? 我可以创造这种行为吗?或者这是语言的限制吗?

public class A
{
    public void methodA()
    {
        Console.WriteLine("I am class A!");
    }
    public void DoA()
    {
        Console.Write("The type of this: " + this.GetType() + " - ");
        this.methodA();
    }
}
public class B : A
{
    public void methodA()
    {
        Console.WriteLine("I am class B!");
    }
}
public class C : B
{
    public void methodA()
    {
        Console.WriteLine("I am class C!");
    }
}


class Program
{
    static void Main(string[] args)
    {
        C first = new C();
        A second = new C();
        dynamic third = new C();

        //Show the types of both

        Console.WriteLine(first.GetType() + "\n" + second.GetType() + "\n" + third.GetType());
        first.methodA();
        second.methodA();
        third.methodA();

        first.DoA();
        second.DoA();
        third.DoA();



        Console.ReadLine();
    }

Output: 输出:

C
C
C
I am class C!
I am class A!
I am class C!
The type of this: C - I am class A!
The type of this: C - I am class A!
The type of this: C - I am class A!

Can I create such behavior or is this a limitation of the language? 我可以创造这种行为吗?或者这是语言的限制吗?

You can create such behavior. 您可以创建这种行为。 In order to do so, you need to make your methods virtual. 为此,您需要使您的方法虚拟化。 This will give you that behavior, without using dynamic at all. 这将为您提供这种行为,而根本不用动态。

public class A
{
    public virtual void methodA()
    {
        Console.WriteLine("I am class A!");
    }

Then, later: 然后,稍后:

public class B : A
{
    public override void methodA()
    {
        Console.WriteLine("I am class B!");
    }
}

In C#, you must explicitly make your methods virtual. 在C#中,您必须显式地将方法虚拟化。 In Java, methods are effectively virtual by default. 在Java中,默认情况下方法实际上是虚拟的。 This isn't a limitation of the language - it's just a difference between the two languages. 这不是语言的限制-只是两种语言之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM