继承问题:如何从基类实例化派生类 [英] Inheritance question: How to instantiate derived class from base class

查看:88
本文介绍了继承问题:如何从基类实例化派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常引人入门的面向对象的问题,但它多年来一直在唠叨我,因为我从来没有找到过

正确的答案,我不得不用非最佳代码解决它。

但是,我怀疑必须有一个适当的面向对象的方法来解决
这个问题。也许有人可能会帮助我。


通常我需要将声明为基本类型的对象转换为其派生的一个

对象类。这是我的非最优惠代码(我现在正在做的)的一个例子。想象一下,

基类和派生类都比

复杂得多,这有几十个属性。


/ * ******* /


类形状:System.Object

{

公共十进制XCoordinate;

公共小数YCoordinate;

}


class Circle:Shape

{

公共十进制周长;

}


void main()

{

//声明一个新的Shape变量

Shape MyShape = new Shape();

MyShape.XCoordinate = 99;

MyShape.YCoordinate = 99;


//各种编程步骤

// ...


//我现在决定我需要MyShape成为Circle类型

Circle MyCircle = new Circle();


//将MyShape的所有属性复制到MyCircle

MyCircle.XCoordinate = MyShape.XCoordinate;

MyCircle.YCoordinate = MyShape.YCoordinate;

MyCircle.C ircumference = 500;

}


/ ******** /


我需要什么要知道是什么是创建新Circle的最佳方法

对象,它是MyShape所有属性的精确副本。

显然,将所有属性从一个复制到一个另一个不是理想的
,因为如果以后我添加了Shape类的新属性,我想要将它的值复制到MyCircle,而不更新

高于代码。


当我尝试执行如下行:


MyCircle =(Circle)MyShape;


这显然不起作用,并抛出运行时错误:发生''System.InvalidCastException''的第一个

机会异常。 />
但是,我确信必须有一个正确的方法,除了

手动创建一个新实例并复制每个

属性。


感谢您的帮助。

解决方案

我假设您出于某种原因,不能提前知道您想要成为一个圆形的形状。如果是这种情况,您可以首先将其声明为

Circle,然后将其视为一个Shape,直到您需要它为

为圆形(例如,类型为Shape的数组。


根据上述假设,我所知道的最佳解决方案是在Circle中使用

转换函数:


public Circle ConvertFromShape(形状)

{

Circle c = new Circle();

c。 XCoordinate = shape.XCoordinate;

c.YCoordinate = shape.YCoordinate;

返回c;

}


现在,当你需要将Shape转换为Circle时,你可以这样做:


Shape s = new Shape();

。 ...

....

Circle c = c.ConvertFromShape(s);

....

....


或者你当然可以让它静止。并且你可以使用更好的构造函数使上面的代码变得更漂亮:但我认为你不需要我

来告诉你。


这可能没有效率,但它更漂亮。


HTH

彼得

" Anthony Greene" < an ************ @ hotmail.comwrote in message

news:11 ****************** ***@n33g2000cwc.googlegro ups.com ...


这可能是一个非常介绍性的面向对象的问题,但它是

多年来我一直在唠叨我,因为我从来没有找到正确的答案,我不得不用非最佳代码解决它。

但是,我怀疑必须有一个适当的面向对象的方法来解决这个问题。也许有人可能会帮助我。


通常我需要将声明为基本类型的对象转换为其派生的一个

对象类。这是我的非最优惠代码(我现在正在做的)的一个例子。想象一下,

基类和派生类都比

复杂得多,这有几十个属性。


/ * ******* /


类形状:System.Object

{

公共十进制XCoordinate;

公共小数YCoordinate;

}


class Circle:Shape

{

公共十进制周长;

}


void main()

{

//声明一个新的Shape变量

Shape MyShape = new Shape();

MyShape.XCoordinate = 99;

MyShape.YCoordinate = 99;


//各种编程步骤

// ...


//我现在决定我需要MyShape成为Circle类型

Circle MyCircle = new Circle();


//将MyShape的所有属性复制到MyCircle

MyCircle.XCoordinate = MyShape.XCoordinate;

MyCircle.YCoordinate = MyShape.YCoordinate;

M yCircle.Circumference = 500;

}


/ ******** /


什么我需要知道创建一个新的Circle

对象的最佳方法是什么,它是MyShape所有属性的精确副本。

显然,复制所有属性一个到另一个不是理想的
,因为如果以后我添加Shape类的新属性,我会想要将它的值复制到MyCircle,而不更新

高于代码。


当我尝试执行如下行:


MyCircle =(Circle)MyShape;


这显然不起作用,并抛出运行时错误:发生了''System.InvalidCastException''类型的第一个

机会异常。

但是,我确信必须有一个正确的方法,除了

手动创建一个新实例并复制每一个

物业。


感谢您的帮助。


hi Anthony,

Anthony Greene写道:


我需要知道的是创建一个新的Circle

对象的最佳方法是什么,它是MyShape所有属性的精确副本。



我会使用某种复制构造函数,未经测试:


class Shape

{

public void CopyTo(Shape toShape){}

}


class Circle:Shape

{

public Circle(){};

public Circle(Shape fromShape){fromShape.CopyTo((Shape)this)};

公共小数周长;

}


更改Shape的属性时,您只需更改CopyTo()

方法。

mfG

--stefan< - -


Anthony Greene写道:


这可能是一个非常介绍性的面向对象的问题,但它<多年来,
一直在唠叨我,因为我从来没有找到正确的答案,所以我必须以非最优的方式解决这个问题。代码。

但是,我怀疑必须有一个适当的面向对象的方法来解决这个问题。
。也许有人可能会帮助我。


通常我需要将声明为基本类型的对象转换为其派生的一个

对象类。这是我的非最优惠代码(我现在正在做的)的一个例子。想象一下,

基类和派生类都比

复杂得多,这有几十个属性。


/ * ******* /


类形状:System.Object

{

公共十进制XCoordinate;

公共小数YCoordinate;

}


class Circle:Shape

{

公共十进制周长;

}


void main()

{

//声明一个新的Shape变量

Shape MyShape = new Shape();

MyShape.XCoordinate = 99;

MyShape.YCoordinate = 99;


//各种编程步骤

// ...


//我现在决定我需要MyShape成为Circle类型

Circle MyCircle = new Circle();


//将MyShape的所有属性复制到MyCircle

MyCircle.XCoordinate = MyShape.XCoordinate;

MyCircle.YCoordinate = MyShape.YCoordinate;

M yCircle.Circumference = 500;

}


/ ******** /


什么我需要知道创建一个新的Circle

对象的最佳方法是什么,它是MyShape所有属性的精确副本。

显然,复制所有属性一个到另一个不是理想的
,因为如果以后我添加Shape类的新属性,我会想要将它的值复制到MyCircle,而不更新

高于代码。


当我尝试执行如下行:


MyCircle =(Circle)MyShape;


这显然不起作用,并抛出运行时错误:发生了''System.InvalidCastException''类型的第一个

机会异常。

但是,我确信必须有一个正确的方法,除了

手动创建一个新实例并复制每一个

物业。


谢谢你的帮助。



你可以用反射来做,但是这种类型的东西有原因

并不容易,它''通常不是一个好主意,当然不像你的Circle / Shape例子那么简单。
。如果你想能够从一个Shape创建一个Circle

,你应该明确如何完成它,这意味着明确

说明Shape的哪些属性被复制到圈子和如何。你可以通过给Circle一个非默认的
构造函数来接收Circle的消费者,这样就可以让Circle的消费者更容易了,这个构造函数接受了一个Shape的实例,从而允许消费者

写Circle MyCircle = new Circle(MyShape);.但是在那个

构造函数中,你将根据需要手动设置Circle的形状

的属性。在你的简单例子中,可能不清楚为什么这种类型的构造是自动的并不是一个好主意。有很多真实世界的例子,其中自动化这种类型的对象构造

会导致问题。


如果你确定的话你总是希望你的基础形状的属性直接复制

,你可以使用上面的变体。那就是给

塑造一个复制构造函数,并让Circle构造函数接收到基础Shape对象的

Shape委托。例如:


公共类形状

{

公共小数X;

公共小数Y;


public Shape(){}

公共形状(形状)

{

this .X = shape.X;

this.Y = shape.Y;

}

}


公共班圈

{

公共小数C;


公共圈(){}

public Circle(形状):base(shape){}

public Circle(圆圈):base(circle)

{

this.C = circle.C;

}

}

-

Tom Porterfield


This is probably a very introductory object-oriented question, but it
has been nagging me for years, and since I''ve never been able to find
the right answer, I''ve had to work around it with non-optimal code.
However, I suspect there must be a proper object-oriented approach to
solving this problem. Perhaps someone might be able to help me out.

Oftentimes I need to convert an object declared as a base type into a
object of one of its derived classes. Here is an example of my non-
optimal code (what I am doing now). Imagine that
both the base class and derive class are much more complicated than
this with dozens of properties.

/********/

class Shape : System.Object
{
public decimal XCoordinate;
public decimal YCoordinate;
}

class Circle : Shape
{
public decimal Circumference;
}

void main ()
{
//declare a new variable of type Shape
Shape MyShape = new Shape();
MyShape.XCoordinate = 99;
MyShape.YCoordinate = 99;

//various programming steps
// ...

//I now decide i need MyShape to be of type Circle
Circle MyCircle = new Circle();

//copy all properites of MyShape into MyCircle
MyCircle.XCoordinate = MyShape.XCoordinate;
MyCircle.YCoordinate = MyShape.YCoordinate;
MyCircle.Circumference = 500;
}

/********/

What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.
Obviously, copying all properties over from one to the other is not
ideal, because if later I add new properties of the Shape class, I
would want its values copied over to MyCircle, without updating the
above code.

When I try to execute a line like:

MyCircle = (Circle)MyShape;

That obviously does not work, and throws a runtime error: "A first
chance exception of type ''System.InvalidCastException'' occurred".
However, I am sure there must be a proper approach to this, other than
manually creating a new instance and copying over each and every
property.

Thank you for your help.

解决方案

I''m assuming that you cannot, for some reason, know in advance that you want
your shape to be a Circle. If that were the case you could declare it as a
Circle in the first place and just treat it as a Shape until you need it to
be a Circle (e.g. in Arrays of type Shape).

With the above assumption, the best solution I know of is to have a
conversion function in Circle:

public Circle ConvertFromShape(Shape shape)
{
Circle c = new Circle();
c.XCoordinate = shape.XCoordinate;
c.YCoordinate = shape.YCoordinate;
return c;
}

Now, when you need to convert a Shape to a Circle, you can do:

Shape s = new Shape();
....
....
Circle c = c.ConvertFromShape(s);
....
....

Or you might make it static, of course. And you can make the code above
even prettier with good use of constructors: but I assume you don''t need me
to tell you that.

It''s probably no more efficient, but it is a bit prettier.

HTH
Peter
"Anthony Greene" <an************@hotmail.comwrote in message
news:11*********************@n33g2000cwc.googlegro ups.com...

This is probably a very introductory object-oriented question, but it
has been nagging me for years, and since I''ve never been able to find
the right answer, I''ve had to work around it with non-optimal code.
However, I suspect there must be a proper object-oriented approach to
solving this problem. Perhaps someone might be able to help me out.

Oftentimes I need to convert an object declared as a base type into a
object of one of its derived classes. Here is an example of my non-
optimal code (what I am doing now). Imagine that
both the base class and derive class are much more complicated than
this with dozens of properties.

/********/

class Shape : System.Object
{
public decimal XCoordinate;
public decimal YCoordinate;
}

class Circle : Shape
{
public decimal Circumference;
}

void main ()
{
//declare a new variable of type Shape
Shape MyShape = new Shape();
MyShape.XCoordinate = 99;
MyShape.YCoordinate = 99;

//various programming steps
// ...

//I now decide i need MyShape to be of type Circle
Circle MyCircle = new Circle();

//copy all properites of MyShape into MyCircle
MyCircle.XCoordinate = MyShape.XCoordinate;
MyCircle.YCoordinate = MyShape.YCoordinate;
MyCircle.Circumference = 500;
}

/********/

What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.
Obviously, copying all properties over from one to the other is not
ideal, because if later I add new properties of the Shape class, I
would want its values copied over to MyCircle, without updating the
above code.

When I try to execute a line like:

MyCircle = (Circle)MyShape;

That obviously does not work, and throws a runtime error: "A first
chance exception of type ''System.InvalidCastException'' occurred".
However, I am sure there must be a proper approach to this, other than
manually creating a new instance and copying over each and every
property.

Thank you for your help.



hi Anthony,

Anthony Greene wrote:

What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.

I would use some kind of "copy" constructor, not tested:

class Shape
{
public void CopyTo(Shape toShape) {}
}

class Circle : Shape
{
public Circle() {};
public Circle(Shape fromShape) { fromShape.CopyTo((Shape)this) };
public decimal Circumference;
}

When changing properties of Shape you just have to change the CopyTo()
method.
mfG
--stefan <--


Anthony Greene wrote:

This is probably a very introductory object-oriented question, but it
has been nagging me for years, and since I''ve never been able to find
the right answer, I''ve had to work around it with non-optimal code.
However, I suspect there must be a proper object-oriented approach to
solving this problem. Perhaps someone might be able to help me out.

Oftentimes I need to convert an object declared as a base type into a
object of one of its derived classes. Here is an example of my non-
optimal code (what I am doing now). Imagine that
both the base class and derive class are much more complicated than
this with dozens of properties.

/********/

class Shape : System.Object
{
public decimal XCoordinate;
public decimal YCoordinate;
}

class Circle : Shape
{
public decimal Circumference;
}

void main ()
{
//declare a new variable of type Shape
Shape MyShape = new Shape();
MyShape.XCoordinate = 99;
MyShape.YCoordinate = 99;

//various programming steps
// ...

//I now decide i need MyShape to be of type Circle
Circle MyCircle = new Circle();

//copy all properites of MyShape into MyCircle
MyCircle.XCoordinate = MyShape.XCoordinate;
MyCircle.YCoordinate = MyShape.YCoordinate;
MyCircle.Circumference = 500;
}

/********/

What I need to know is what is the best way to create a new Circle
object which is an exact copy of all properties of MyShape.
Obviously, copying all properties over from one to the other is not
ideal, because if later I add new properties of the Shape class, I
would want its values copied over to MyCircle, without updating the
above code.

When I try to execute a line like:

MyCircle = (Circle)MyShape;

That obviously does not work, and throws a runtime error: "A first
chance exception of type ''System.InvalidCastException'' occurred".
However, I am sure there must be a proper approach to this, other than
manually creating a new instance and copying over each and every
property.

Thank you for your help.

You could do it using reflection, but there''s a reason this type of stuff
isn''t easy, it''s usually not a good idea and certainly not nearly as simple
as your Circle/Shape example. If you want to be able to create a Circle
from a Shape, you should be explicit in how that is done, meaning clearly
stating which properties of Shape get copied to Circle and how. You can
make this easier on consumers of Circle by giving Circle a non-default
constructor which takes in an instance of Shape, thus allowing the consumer
to write Circle MyCircle = new Circle(MyShape);. But within that
constructor you''ll be manually setting the properties of Circle from Shape
as are appropriate. In your simplistic example it may not be clear why it''s
not a good idea to have this type of construction be automatic. There are
many real-world examples where automating this type of object construction
would cause problems.

If you are sure you always want the properties from your base Shape copied
directly, you could use a variation on the above. That would be to give
Shape a copy constructor, and have the Circle constructor that takes in a
Shape delegate that to the base Shape object. Ex:

public class Shape
{
public decimal X;
public decimal Y;

public Shape() {}
public Shape(Shape shape)
{
this.X = shape.X;
this.Y = shape.Y;
}
}

public class Circle
{
public decimal C;

public Circle(){}
public Circle(Shape shape) : base (shape) {}
public Circle(Circle circle) : base (circle)
{
this.C = circle.C;
}
}
--
Tom Porterfield


这篇关于继承问题:如何从基类实例化派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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