DerivedClass DerObj = new BaseClass();为什么它不工作请任何人告诉我原因 [英] DerivedClass DerObj = new BaseClass(); why its not working please any one tell me the reason

查看:92
本文介绍了DerivedClass DerObj = new BaseClass();为什么它不工作请任何人告诉我原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 DerivedClass DerObj =  new  BaseClass(); 



为什么是它不工作?任何人都可以告诉我原因吗?

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 MyConsole
{
class Program
{
静态 void Main( string [] args)
{
BaseClass BaseObj = new DerivedClass();
// 任何人请告诉我为什么它不工作我不想要花费我想要理由。
DerivedClass DerObj = new BaseClass();
}
}
public class BaseClass
{
}
public class DerivedClass:BaseClass
{
}
}

解决方案

您需要了解运行时类型 vs 编译时间类型。和继承。

BaseObj 编译时类型 BaseClass ,但实际 runtime 类型为 DerivedClass 。通过继承, DerivedClass 可以有一些成员不在 BaseClass 中,但该变量不允许您访问这些添加的成员。这一切都完全可以接受。这就是多态的工作方式。



相反的情况是不可接受的。该变量可以访问仅在派生类中添加的某些成员。但实际的运行时类型是基类。因此,您可以访问实际上不存在的成员。这将是灾难。



这是清楚的吗?



-SA


Sergey Alexandrovich Kryukov的解决方案2非常好,我想添加一些额外的链接:

继承(C#编程指南) [ ^ ]

多态性(C#编程指南) [ ^ ]

继承简介,C#中的多态性 [ ^ ]

BaseClass baseObj = new DerivedClass();



有效,因为根据定义, DerivedClass对象隐式为BaseClass。



 DerivedClass derObj =  new  BaseClass(); 



不起作用,因为BaseClass对象不是系统地DerivedClass;它可能是另一个定义为从BaseClass派生的对象,但与DerivedClass不同。


DerivedClass DerObj = new BaseClass();


Why is it not working? Can anyone please tell me the reason?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            BaseClass BaseObj = new DerivedClass();
            //any one please tell why its not working i dont want do costing i want reason.
            DerivedClass DerObj = new BaseClass();
        }
    }
    public class BaseClass
    {     
    }
    public class DerivedClass : BaseClass
    {
    }
}

解决方案

You need to understand the runtime types vs compile-time types. And inheritance.
BaseObj has the compile-time type BaseClass, but actual runtime type is DerivedClass. By inheritance, DerivedClass can have some members not present in BaseClass, but the variable does not give you access to these added members. It's all fully acceptable. This is how polymorphism works.

The opposite situation would be unacceptable. The variable could get access to some members added only in derived class. But the actual runtime type is the base class. So, you would get access to the members which physically do not exist. It would be the disaster.

Is that clear?

—SA


Solution 2 by Sergey Alexandrovich Kryukov is very good and i would like to add some extra links:
Inheritance (C# programming guide)[^]
Polymorphism (C# Programming Guide)[^]
Introduction to inheritance, polymorphism in C#[^]


BaseClass baseObj = new DerivedClass();


works because, by definition, a DerivedClass object is implicitly a BaseClass.

DerivedClass derObj = new BaseClass();


does not work because a BaseClass object is not systematically a DerivedClass ; it could be another object defined as derived from BaseClass, but different from DerivedClass.


这篇关于DerivedClass DerObj = new BaseClass();为什么它不工作请任何人告诉我原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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