[C#]运行时和编译时变量? [英] [C#] run-time and compile-time variable ?

查看:115
本文介绍了[C#]运行时和编译时变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace CSharp_Practicing
{

    class Program
    {

        static void Main(string[] args)
        {
            Animal a = new Dog(); // THE QUESTION IS ABOUT THIS VARIABLE !!!
        }
    }

    class Animal { }
    class Dog : Animal
    {
        public void SayBau()
        {
            Console.WriteLine("Baaaaaau");
        }
    }
    class Cat : Animal
    {
        public void SayMaw()
        {
            Console.WriteLine("Mawwwwwwwwwwwww");
        }
    }
}





编译时变量a的类型为Animal在运行时它的类型狗?我是对的还是我理解错了?



我尝试了什么:



在CodeProject.com中提出问题



The variable a in compile-time is of type Animal and in run-time its of type Dog ? Am I right or I am understanding it wrongly ?

What I have tried:

Asking question here in CodeProject.com

推荐答案

Animal a = new Dog(); 





您正在创建两件事,即创建一个对象(Dog)和一个引用变量(a)。对象是一只狗,它永远是一只狗,而作为一只狗的一部分也意味着一只动物。您创建的变量是Animal类型,并指向Dog对象的Animal部分。由于对象a指向的是狗,你可以这样做





You're creating two things, you're creating an object (Dog) and a reference variable (a). The object is a Dog, it will always be a Dog and part of being a Dog means also being an Animal. The variable you create is of type Animal and is pointing to the Animal part of the Dog object. As the object "a" points to is a Dog you could do this

Dog d = (Dog)a;





您仍然只有一个对象(Dog),但是您创建了另一个指向对象Dog部分的引用变量(d)。所以现在你有一个对象(Dog)和两个指向这个对象的引用变量; a正在查看动物部分并且只能访问动物属性和方法,而d正在查看Dog部分并且可以访问Dog部分或Animal部分的任何属性或方法(因为Dog继承Animal ,狗也被认为是动物。



现在考虑这个





You still only have one object (Dog), however you have created another reference variable (d) that points to the Dog part of the object. So now you have one object (Dog) and two reference variables that point to this object; "a" which is looking at the Animal part and can only access Animal properties and methods, and "d" which is looking at the Dog part and can access any property or method on the Dog part or the Animal part (as Dog inherits Animal, Dog is considered an Animal too).

Now consider this

Cat c = (Cat)a;





这会编译吗?是。编译器只知道a必须是动物,所以*可能*它可能是一只猫,但它在编译时不知道,所以它给你带来怀疑的好处。但是在运行时它会抛出一个错误,因为a指向的对象中没有Cat部分,因为a指向Dog。



Will this compile? Yes. The compiler only knows that "a" must be an animal, so it is *possible* that it might be a Cat but it doesn't know at compile time so it gives you the benefit of the doubt. However at run-time it will throw an error as there is no "Cat" part of the object that "a" points to, as "a" points to a Dog.


粗略地说,

您可以在每个上下文中使用它<* c $ c> Animal 是允许的(因此它的'编译时'类型),但它的实际类型,在运行时,多态,



注意,你的班级层次结构不利用多态性。
Roughly speaking, yes.
You may use it in every context a Animal is allowed (hence its 'compile-time' type), however its actual type, at runtime, is, polymorphically, Dog.

Note, your class hierarchy doesn't take advantage of polymorphism.


它始终是一个Dog,因为你在 new 语句中以这种方式创建它。实际上,变量在编译时不是任何东西,因为它在程序运行之前不存在。
It's always a Dog, as you created it that way in your new statement. In reality the variable is not anything at compile time since it does not exist until the program is run.


这篇关于[C#]运行时和编译时变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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