将父级转换为子级,但将数据保留在父级中并转移到子级 [英] Cast parent to child but keeping data in parent class and transfering to child

查看:80
本文介绍了将父级转换为子级,但将数据保留在父级中并转移到子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将父类强制转换为子类但不覆盖父类中的现有数据?



如果我有这个数据,那么已经在基类实例中的数据将是用新的Derived()覆盖。



 Base derivedInstance = new Derived(); 
派生商品=(派生)derivedInstance;







希望您理解我的问题?



我尝试过:



基地derivedInstance = new Derived(); 
派生好=(衍生)derivedInstance;

解决方案

当你施展东西时,你不要扔掉任何东西从对象来看,它实际上并没有以任何方式改变对象。所有这一切都是根据你的代码改变你可以做的事情。

想想汽车片刻。

你有一个汽车课:

  public   class  Car 
{
public int CountWheels { get { return 4 ; }
}

您可以使用它为每个制造商创建派生类:

 公开 福特:Car 
{
public bool MadeInGermany { get { return false ;}}
}
public class MercedesBenz:Car
{
public bool MadeInAmerica {< span class =code-keyword> get { return false ;}}
}

你可以操纵福特:

 Car thatCar =  new 福特(); 
福特thisCar =(福特)那汽车;
Console.WriteLine( {0}:{1},thisCar.CountWheels, thisCar.MadeInGermany);

并且铸造它对它没有任何作用 - 它仍然是汽车和福特。

你不能做的是铸造梅赛德斯到福特:

 Car thatCar =  new  MercedesBenz(); 
福特thisCar =(福特)那汽车;
Console.WriteLine( {0}:{1},thisCar.CountWheels, thisCar.MadeInGermany);

系统将抛出一个例外,因为福特和梅赛德斯不一样 - 你不能拿梅赛德斯徽章,把它粘在福特上,并期望人们相信它! :笑:



如果在执行此操作之前在derivedInstance中有基类的实例:

 derivedInstance = new Derived(); 

然后系统将在您加载新值时放弃对该实例的引用 - 但它不会更改该实例。在您的代码中

 Base derivedInstance = new Derived(); 

derivedInstance是一个新变量,因此它不包含任何先前的实例,定义变量的行设置它的第一个值。


您可以在Derived类中使用不同的构造函数,请参见此处的示例:

如何:使用对象初始化程序初始化对象(C#编程指南) Microsoft Docs [ ^ ]

你可能也对 ICloneable ,请参见此处的示例: [ ^ ]

但这并不适用于所有情况,因此您的序列化选项可能会更好......


我找到了这个解决方案:



 var serializedParent = JsonConvert.SerializeObject(parentInstance); 
子c = JsonConvert.DeserializeObject< Child>(serializedParent);





这将保留来自Parent类的记录:)


How to cast parent class to child but not overwriting existing data in parent class ?

If I have this than data which is already in Base class instance will be overwritten with new Derived().

Base derivedInstance = new Derived();
Derived good = (Derived)derivedInstance;




Hope You understand my question ?

What I have tried:

Base derivedInstance = new Derived();
Derived good = (Derived)derivedInstance;

解决方案

When you cast something, you don;t "throw away" anything from the object, it doesn't actually change the object in any way at all. All it does is change what you can do with it in terms of your code.
Think about cars for a moment.
You have a Car class:

public class Car
   {
   public int CountWheels { get { return 4; } }
   }

And you use that to create a derived class for each manufacturer:

public class Ford : Car
   {
   public bool MadeInGermany { get { return false;} }
   }
public class MercedesBenz : Car
   {
   public bool MadeInAmerica { get { return false;} }
   }

You can manipulate a Ford:

Car thatCar = new Ford();
Ford thisCar = (Ford) thatCar;
Console.WriteLine("{0}:{1}", thisCar.CountWheels, thisCar.MadeInGermany);

And casting it doesn't do anything to it - it remains both a Car and a Ford.
What you can't do is cast a Mercedes to a Ford:

Car thatCar = new MercedesBenz();
Ford thisCar = (Ford) thatCar;
Console.WriteLine("{0}:{1}", thisCar.CountWheels, thisCar.MadeInGermany);

And the system will thrown an exception, because a Ford and a Mercedes are not the same - you can't take a Mercedes badge, glue it to a ford and expect people to believe it! :laugh:

If you have an instance of the base class in derivedInstance before you do this:

derivedInstance = new Derived();

Then the system will discard the reference to that instance when you load the new value - but it won't change that instance. In your code

Base derivedInstance = new Derived();

derivedInstance is a new variable, so it contained no previous instance anyway, the line defining the variable sets it's first value.


You could use a different constructor in your Derived class, see example here:
How to: Initialize Objects by Using an Object Initializer (C# Programming Guide) | Microsoft Docs[^]
You might also be interested in ICloneable, see example here: [^]
But this does not work in all cases, so your serializing option might be better ...


I found this solution:

var serializedParent = JsonConvert.SerializeObject(parentInstance); 
 Child c  = JsonConvert.DeserializeObject<Child>(serializedParent);



this will keep records from Parent class :)


这篇关于将父级转换为子级,但将数据保留在父级中并转移到子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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