且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

继承问题:如何从基类实例化派生类

更新时间:2023-02-15 15:19:19

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