[英]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.