克隆其基础对象具有许多字段的派生对象 [英] Cloning derived objects whose base objects have many fields

查看:115
本文介绍了克隆其基础对象具有许多字段的派生对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为Vehicle的类,其中包含许多字段。现在我创建了一个新的

类派生自Vehicle的Car。我想创建一种方法将

数据从Vehicle实例复制到Car实例。在Clone()语句中写一长串

赋值我唯一的选择吗?


我知道我可以使用MemberwiseClone()编写一个Clone方法,但我的理解是,由MemberwiseClone创建的对象具有相同的

类型。

解决方案

KK,


这不是个好主意。如果您有一个类,Airplane,派生来自Vehicle的
,然后将其传递给要克隆的Car实例,该怎么办?一架

飞机绝对不是一辆车,所以它不应该真的有效。


你想做什么?如果有的话,汽车应该克隆一辆

汽车,或任何来自汽车的东西。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" KK" < kkaitan [at] gmail [dot] com>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

假设我有一个名为Vehicle的类,有很多字段。现在我创建一个来自Vehicle的新类
类Car。我想制作一种方法将
数据从Vehicle实例复制到Car实例。在Clone()语句中编写一个长篇系列
分配我唯一的选择吗?

我知道我可以使用MemberwiseClone()编写一个Clone方法,但我的<理解是由MemberwiseClone创建的对象具有相同的类型。



Nicholas,


对不起,我想我还不清楚。我说我有一个

车辆(基类)的实例。现在我想构建一个新的汽车实例

,其基类字段等于Vehicle实例。我猜它是像汽车的复制构造函数那样的b $ b,除了我没有使用另一辆汽车作为复制的基础,而是一辆车。


希望更有意义。再次感谢!


- KK

Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

KK,

这不是个好主意。如果您有一个类,Airplane,派生来自Vehicle,然后将其传递给要克隆的Car实例,该怎么办?一架
飞机绝对不是一辆车,所以它不应该真的有用。

你想要做什么?如果有的话,汽车应该克隆
a汽车或任何来自汽车的东西。

希望这会有所帮助。

-
- Nicholas Paldino [。 NET / C#MVP]
- mv*@spam.guard.caspershouse.com
KK < kkaitan [at] gmail [dot] com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

假设我有一个叫做的课程有很多领域的车辆。现在我创建一个来自Vehicle的新类
类Car。我想制作一种方法将
数据从Vehicle实例复制到Car实例。在Clone()语句中编写一个长篇系列
分配我唯一的选择吗?

我知道我可以使用MemberwiseClone()编写一个Clone方法,但我的<理解是,MemberwiseClone创建的对象是
相同的类型。




< blockquote> KK,


确实如此,但你的逻辑是错误的,因为一辆车可以是什么东西

不是汽车,但汽车总是一辆车。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" KK" < kkaitan [at] gmail [dot] com>在消息中写道

新闻:ON ************** @ TK2MSFTNGP10.phx.gbl ...

Nicholas,
抱歉,我想我不清楚。我说我有一个
车辆的实例(基类)。现在我想构建一个
Car
的新实例,其基类字段等于Vehicle实例。我想这就像是Car的复制构造器一样,除了我没有使用另一辆车
作为复制的基础,而是车辆。

希望更有意义。再次感谢!

- KK

" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>在
消息新闻中写道
:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

KK,

这不是个好主意。如果您有一个类,Airplane,派生来自Vehicle,然后将其传递给要克隆的Car实例,该怎么办?一架
飞机绝对不是一辆车,所以它不应该真的有用。

你想要做什么?如果有的话,汽车应该克隆


汽车,或任何来自汽车的东西。

希望这会有所帮助。 />
-
- Nicholas Paldino [.NET / C#MVP]
- mv * @ spam .guard.caspershouse.com

KK < kkaitan [at] gmail [dot] com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

>假设我有一个名为Vehicle的类,有很多字段。现在我做了一个
>新
>派生自Car的车辆。我想制作一个方法来
>复制
>
>从Vehicle实例到Car实例的数据。写得很长
>系列
>
> Clone()语句中的赋值是我唯一的选择吗?
>
>我知道我可以使用MemberwiseClone()编写一个Clone方法,但我的
>理解是由MemberwiseClone创建的对象具有相同的>类型。
>
>





Suppose I have a class called Vehicle with many fields. Now I make a new
class derived from Vehicle called Car. I''d like to make a method to copy the
data from a Vehicle instance to a Car instance. Is writing a long series of
assignments in a Clone() statement my only option?

I know that I can write a Clone method using MemberwiseClone(), but my
understanding is that the object created by MemberwiseClone is of the same
type.

解决方案

K.K.,

This isn''t a good idea. What if you have a class, Airplane, derived
from Vehicle, and then pass that to a Car instance to be cloned? An
Airplane definitely isn''t a car, so it shouldn''t really work.

What is it that you are trying to do? If anything, a Car should clone a
Car, or anything deriving from Car.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Suppose I have a class called Vehicle with many fields. Now I make a new
class derived from Vehicle called Car. I''d like to make a method to copy
the
data from a Vehicle instance to a Car instance. Is writing a long series
of
assignments in a Clone() statement my only option?

I know that I can write a Clone method using MemberwiseClone(), but my
understanding is that the object created by MemberwiseClone is of the same
type.



Nicholas,

Sorry, I guess I wasn''t clear. I''m saying that I have an instance of a
Vehicle (the base class). Now I''d like to construct a new instance of a Car
whose base-class fields are equal to the Vehicle instance. I guess it''s sort
of like a copy constructor for Car, except that I''m not using another Car as
the basis for the copying, but rather a Vehicle.

Hope that makes a little more sense. Thanks again!

- KK
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...

K.K.,

This isn''t a good idea. What if you have a class, Airplane, derived
from Vehicle, and then pass that to a Car instance to be cloned? An
Airplane definitely isn''t a car, so it shouldn''t really work.

What is it that you are trying to do? If anything, a Car should clone a Car, or anything deriving from Car.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Suppose I have a class called Vehicle with many fields. Now I make a new
class derived from Vehicle called Car. I''d like to make a method to copy
the
data from a Vehicle instance to a Car instance. Is writing a long series
of
assignments in a Clone() statement my only option?

I know that I can write a Clone method using MemberwiseClone(), but my
understanding is that the object created by MemberwiseClone is of the same type.




KK,

It does, but your logic is wrong, because a Vehicle can be something
that is not a Car, but a Car is always a Vehicle.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...

Nicholas,

Sorry, I guess I wasn''t clear. I''m saying that I have an instance of a
Vehicle (the base class). Now I''d like to construct a new instance of a
Car
whose base-class fields are equal to the Vehicle instance. I guess it''s
sort
of like a copy constructor for Car, except that I''m not using another Car
as
the basis for the copying, but rather a Vehicle.

Hope that makes a little more sense. Thanks again!

- KK
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP09.phx.gbl...

K.K.,

This isn''t a good idea. What if you have a class, Airplane, derived
from Vehicle, and then pass that to a Car instance to be cloned? An
Airplane definitely isn''t a car, so it shouldn''t really work.

What is it that you are trying to do? If anything, a Car should
clone


a

Car, or anything deriving from Car.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

> Suppose I have a class called Vehicle with many fields. Now I make a
> new
> class derived from Vehicle called Car. I''d like to make a method to
> copy
> the
> data from a Vehicle instance to a Car instance. Is writing a long
> series
> of
> assignments in a Clone() statement my only option?
>
> I know that I can write a Clone method using MemberwiseClone(), but my
> understanding is that the object created by MemberwiseClone is of the same > type.
>
>





这篇关于克隆其基础对象具有许多字段的派生对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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