简体   繁体   English

为什么当我尝试从父类(向下转换)创建子 class 时,我得到 null object

[英]why is it when I try to create a child class from a parent class(Downcasting), I get a null object

namespace Test
{  
    public class Basic
    {

        public virtual void WriteIt()
        {
            Console.WriteLine("Hi");
        }
    }

    public class Not_So_Basic : Basic
    {
        public override void WriteIt()
        {
            Console.WriteLine("Hi2");
        }
    }
    class Program
    {

        static void Main(string[] args)
        {

            Not_So_Basic obj = new Basic() as Not_So_Basic;
            if(obj==null)
                Console.WriteLine("Error");
            else
            obj.WriteIt();
        }



    }
}

So basically I created these two classes, Basic and Not_So_Basic which is a child class of the Basic class.所以基本上我创建了这两个类,Basic 和 Not_So_Basic,它们是 Basic class 的子 class。

When I try to create an object of the Not_So_Basic class using an object of its parent class by Downcasting it, I always get a null object. When I try to create an object of the Not_So_Basic class using an object of its parent class by Downcasting it, I always get a null object.

I understand by doing this, I'm just referencing my child class object to the parent class object and I get that "child class view", right? I understand by doing this, I'm just referencing my child class object to the parent class object and I get that "child class view", right? I'm not really creating a separate space in heap of a child class based off of the parent class.我并不是真的在基于父 class 的子 class 的堆中创建单独的空间。

I'm kind of new at programming so please be gentle, don't roast me.我是编程新手,所以请温柔一点,不要烤我。 I'm sure there is a rule that I'm breaking.我确信有一条规则我正在打破。 Btw I'm trying to learn to program, I'm not working on a project, just trying to wrap my head around a rule that I'm breaking.顺便说一句,我正在努力学习编程,我不是在做一个项目,只是想把我的脑袋绕在一个我正在打破的规则上。

The code is written in in c#.代码写在 c# 中。

The as operator attempts to cast, but gives you a null object if it can't. as运算符尝试强制转换,但如果不能,则为您提供null object。 So this means the cast failed.所以这意味着演员失败了。

You're casting a Basic object to Not_So_Basic .您正在将Basic object 投射到Not_So_Basic You cannot do that because Basic does not inherit from Not_So_Basic .您不能这样做,因为Basic不继承自Not_So_Basic

To illustrate, all foxes are animals, but not all animals are foxes, so you can't cast an Animal to a Fox .举例来说,所有狐狸都是动物,但并非所有动物都是狐狸,因此您不能将Animal转换为Fox

That said, if you have an actual Not_So_Basic object that's just represented as a Basic object, then you can cast it to Not_So_Basic .也就是说,如果您有一个实际的Not_So_Basic object,它只是表示为Basic object,那么您可以将其转换为Not_So_Basic For example:例如:

Basic basic = new Not_So_Basic();

Not_So_Basic not_so_basic = (Not_So_Basic) basic; //this works

Downcasting is where you take a base class and then try and turn it into a more specific class.向下转换是您采用基础 class,然后尝试将其转换为更具体的 class。 This can be accomplished with using as like this:这可以通过as这样使用来完成:

Not_So_Basic notSoBasic = basic as Not_So_Basic;

The cast will be successful only if basic is referring to an instance of Not_So_Basic只有当basic引用Not_So_Basic的实例时,转换才会成功

//successful cast
Basic basic = new Not_So_Basic();
Not_So_Basic notSoBasic = basic as Not_So_Basic;

OR basic is referring to an instance of Not_So_Basic child. OR basic指的是Not_So_Basic孩子的一个实例。

public class Not_So_Basic_Child : Not_So_Basic
    {
        public override void WriteIt()
        {
            Console.WriteLine("Hi from child");
        }
    }

//another successful cast
Basic basic = new Not_So_Basic_Child();
Not_So_Basic notSoBasic = basic as Not_So_Basic;

If you are trying to cast an instance of a base class to an instance of a child class, as operator will return null如果您尝试将基础 class 的实例转换为子 class 的实例, as操作员将返回null

//unsuccessful cast
Basic basic = new Basic();
Not_So_Basic notSoBasic = basic as Not_So_Basic; //is null

Creating an Object of Child Class with reference to Parent class, result will be always null.参考父 class 创建子 Class 的 Object,结果将始终为 Z37A6259CC0C1DAE299A786648。 We can not create a child class object like that.我们不能像那样创建子 class object。

//unsuccessful cast
Parent parent = new Parent();
Child child = parent as Child; //is null

Correct way:正确方法:

Parent parent= new Child();
Child  child = (Child) parent; //this works

暂无
暂无

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

相关问题 为什么我只得到父类属性和方法,而父类对象正在用子类启动 - Why I'm getting only parent class property & method while parent class object is initiating with child class 如果我从父类中创建子类,则会再次创建父类 - If I create a child class from within a parent class, the parent class gets created again 父类中的公共对象在子类xaml中为null - public object from parent class is null in child class xaml 为什么要创建父类的类型来存储子类的对象? - why to create type of parent class to store the object of child class? 当我尝试使用设计器中的表单类变量时,为什么会出错? - Why I get error when I try to use variable of form class from designer? 我如何访问从单独的类到主类的getter和setter(在C#中向下转换) - How do I get access to getters and setters from a separate class to the main class(with downcasting in C#) 如何获取父类的实例,即使用对象初始化时已启动当前类的类? - How can I get an instance of the parent class, the class that has initiated the current class when using object initialization? 为什么当我尝试使用 MVVM Z3055DD729D79EAAE738 中的命令修改它时,在视图 model 中我的 object 出现 null 错误? - Why I get null error for my object in view model when I try to modify it with command in MVVM WPF? 将基类引用复制到子类时有陷阱吗? (向下转换) - Are there gotchas when copying a base class reference to child class? (downcasting) 为什么当我尝试实例化类实例时,为什么会得到StackOverFlowException? - Why when I try to instantiate a class instance, do I get StackOverFlowException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM