简单继承:从基类实例创建派生类。 [英] Simple Inheritance : Creating derived classes from instances of base class.

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

问题描述



如何从

基类的实例创建派生类的实例,基本上用一些
附加功能。我需要这个的原因是因为我不是

总是能够控制/创建基础

类所具有的所有不同构造函数。我的问题可以在代码中描述如下......


/ *这是一个基本类,有一整堆

构造函数/功能* /

公共类动物

{

公共动物()

{/ ** /}

public Animal(int x)

{/ ** /}


}


/ *这是派生类,它为基础添加了一些功能* /

公共类TaggedAnimal:动物


{

公共字符串GetTag()

{/ ** /}

}

在我的main方法中,我有一大堆Animal类那是

生成的。我想将它们转换为TaggedAnimals并使用它们开始

。我不想只使用另一个方法/类来实现

给定Animal的GetTag()功能(使用动物作为成员
另一个类型的
) )因为有其他控件/类/方法

期望一个动物类型,我想将同一个对象传递给

它们。


所以主要代码看起来像这样:

公共类MyMainClass

{

public void MyMainMethod()

{

/ *动物在这里建造,我在这里没有多少控制权和

不要想要。* /

System.Collections.Arraylist allAnimals =

ThirdPartyORMTool.GetAllAnimals();


/ *但是一次我有实例,我想继承他们

来添加我自己的功能。 TagAllAnimals方法

强制转换/构造/创建动物实例到

TaggedAnimals * /

System.Collections.Arraylist allTaggedAnimals =

TagAllAnimals(allAnimals);

/ *现在我可以使用标记动物进行各种控制来控制/显示基础中的任何功能或者派生类* /

Gridview.DataSource = allTaggedAnimals;


/ *我还可以为任何其他期望使用相同的集合

一个Animal类(基类)* /

ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);

ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);

}


}


我该怎么做呢?基本上,我的第一直觉就是做这样的事情。


公共类TaggedAnimal:动物


{


/ *将基类的实例传递给构造函数* /

public TaggedAnimal(Animal currentAnimal)

{

/ *将基类设置为基类的实例

传入* /

base = currentAnimal;

}


公共字符串GetTag()

{/ ** /}


}

但在C#.net 2.0中,它抱怨base在这个

上下文中无效。


我可能在这里缺少一些微不足道的东西,但是如何设置/控制基类的

实例而不必通过调用其构造函数来创建基类

类参数。或者通过

创建派生类开始。


非常感谢任何帮助。

Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don''t want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don''t have much control here and
don''t want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

推荐答案

< ny ******** @ hotmail.comwrote in message

news:11 *********** **********@a75g2000cwd.googlegro ups.com ...
<ny********@hotmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...



如何创建来自

基类实例的派生类的实例,基本上包含了一些现有的基类,其中包含一些

的附加功能。我需要这个的原因是因为我不是

总是能够控制/创建基础

类所具有的所有不同构造函数。我的问题可以在代码中描述如下......


/ *这是一个基本类,有一整堆

构造函数/功能* /

公共类动物

{

公共动物()

{/ ** /}

public Animal(int x)

{/ ** /}


}
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}



我会在基类Animal类中创建一个复制构造函数,它接收一个

Animal并使用传入的值设置''this'中的所有数据

动物实例。


公共动物(动物副本)

{

this.x = Animal.GetX() ;

//等

}

I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in ''this'' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}


/ *这是派生类,它为base * /

public class TaggedAnimal:Animal


{

公共字符串GetTag()

{/ ** /}


}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}



然后向TaggedAnimal添加一个构造函数接受一个动物。


公共TaggedAnimal(动物一个):基础(一个)

Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)


在我的主要方法中我有生成了一大堆动物类,即
。我想将它们转换为TaggedAnimals并使用它们开始

。我不想只使用另一个方法/类来实现

给定Animal的GetTag()功能(使用动物作为成员
另一个类型的
) )因为有其他控件/类/方法

期望一个动物类型,我想将同一个对象传递给

它们。


所以主要代码看起来像这样:

公共类MyMainClass

{

public void MyMainMethod()

{

/ *动物在这里建造,我在这里没有多少控制权和

不要想要。* /

System.Collections.Arraylist allAnimals =

ThirdPartyORMTool.GetAllAnimals();


/ *但是一次我有实例,我想继承他们

来添加我自己的功能。 TagAllAnimals方法

强制转换/构造/创建动物实例到

TaggedAnimals * /

System.Collections.Arraylist allTaggedAnimals =

TagAllAnimals(allAnimals);
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don''t want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don''t have much control here and
don''t want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);



在这里,对于allAnimals中Animal的每个实例,你将创建一个

TaggedAnimal,将Animal传递给构造函数。 />

In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.


/ *现在我可以使用标记动物进行各种控制来控制/显示基础或派生类中的任何功能* /

Gridview.DataSource = allTaggedAnimals;


/ *我也可以为任何其他人预期使用相同的集合

一个动物class(基类)* /

ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);

ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);


我将如何做到这一点?基本上,我的第一直觉就是做这样的事情。


公共类TaggedAnimal:动物


{


/ *将基类的实例传递给构造函数* /

public TaggedAnimal(Animal currentAnimal)

{

/ *将基类设置为基类的实例

传入* /

base = currentAnimal;

}
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}



你不能像这样分配给基本实例。您可以根据currentAnimal中的相同属性值在base中分配

属性,或者将复制构造函数添加

,如上所述。

You can''t assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.


公共字符串GetTag()

{/ ** /}


}


但是在C#.net 2.0中,它抱怨base在这个

上下文中无效。


我可能会丢失这里有一些简单的东西,但是我如何设置/控制基类的

实例,而不必通过调用带有所需参数的构造函数来创建基类

类。或者通过

创建派生类。
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.



你不能。一旦进入派生类构造函数,就会创建基础

实例。你不能指定一个新的基础实例。

-

Tom Porterfield

You can''t. Once you are within your derived classes constructor, your base
instance is already created. You can''t assign a new base instance.
--
Tom Porterfield


Tom Porterfield写道:
Tom Porterfield wrote:

< ny ******** @ hotmail.comwrote in message

news:11 ** *******************@a75g2000cwd.googlegro ups.com ...
<ny********@hotmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...



如何从

基类的实例创建派生类的实例,基本上用一些额外的功能包装现有的基类

。我需要这个的原因是因为我不是

总是能够控制/创建基础

类所具有的所有不同构造函数。我的问题可以在代码中描述如下......


/ *这是一个基本类,有一整堆

构造函数/功能* /

公共类动物

{

公共动物()

{/ ** /}

public Animal(int x)

{/ ** /}


}
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}



我会在基类Animal类中创建一个复制构造函数,它接收一个

Animal并使用传入的值设置''this'中的所有数据

动物实例。


公共动物(动物副本)

{

this.x = Animal.GetX() ;

//等

}


I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in ''this'' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}


/ *这是派生类,它为base * /

public class TaggedAnimal:Animal


{

公共字符串GetTag()

{/ ** /}


}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}



然后添加一个const带有动物的TaggedAnimal的ructor。


公共TaggedAnimal(动物一个):base(an)


Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)



不幸的是我无法创建复制构造函数。这就是为什么我在

中说原始帖子因为我并不总是能够控制/创建所有

基类具有的不同构造函数。问题是

基础类是在ORM工具的数据访问层中为我自动生成的。我不想修改自动生成的代码。并且

修改/调整ORM以添加自定义代码看起来不是很优雅


Unfortunately I can''t create a copy constructor. Thats why i said in
the original post "because I am not always able to control/create all
the different constructors the base class has.". The problem is that
the base classes are auto generated for me in the data access layer by
the ORM tool. I don''t want to modify auto generated code. And
modifying/tweaking the ORM to add custom code doesn''t seem very elegant
either.


在我的main方法中,我有一大堆动物类,它们是生成的
。我想将它们转换为TaggedAnimals并使用它们开始

。我不想只使用另一个方法/类来实现

给定Animal的GetTag()功能(使用动物作为成员
另一个类型的
) )因为有其他控件/类/方法

期望一个动物类型,我想将同一个对象传递给

它们。


所以主要代码看起来像这样:

公共类MyMainClass

{

public void MyMainMethod()

{

/ *动物在这里建造,我在这里没有多少控制权和

不要想要。* /

System.Collections.Arraylist allAnimals =

ThirdPartyORMTool.GetAllAnimals();


/ *但是一次我有实例,我想继承他们

来添加我自己的功能。 TagAllAnimals方法

强制转换/构造/创建动物实例到

TaggedAnimals * /

System.Collections.Arraylist allTaggedAnimals =

TagAllAnimals(allAnimals);
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don''t want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don''t have much control here and
don''t want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);



在这里,对于allAnimals中Animal的每个实例,你将创建一个

TaggedAnimal,将Animal传递给构造函数。 />


In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.


/ *现在我可以使用标记动物进行各种控制来控制/显示基础或派生类中的任何功能* /

Gridview.DataSource = allTaggedAnimals;


/ *我也可以为任何其他人预期使用相同的集合

一个动物class(基类)* /

ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);

ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);


我将如何做到这一点?基本上,我的第一直觉就是做这样的事情。


公共类TaggedAnimal:动物


{


/ *将基类的实例传递给构造函数* /

public TaggedAnimal(Animal currentAnimal)

{

/ *将基类设置为基类的实例

传入* /

base = currentAnimal;

}
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}



你不能像这样分配给基本实例。您可以根据currentAnimal中的相同属性值在base中分配

属性,或者将复制构造函数添加

,如上所述。


You can''t assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.


公共字符串GetTag()

{/ ** /}


}

但是在C#.net 2.0中,它抱怨base在这个

上下文中是无效的。


我可能会遗漏一些简单的东西,但是如何通过调用带有所需参数的构造函数来设置/控制基类的

实例而不必显式创建基类

类。或者通过

创建派生类。
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.



你不能。一旦进入派生类构造函数,就会创建基础

实例。您不能分配新的基本实例。


You can''t. Once you are within your derived classes constructor, your base
instance is already created. You can''t assign a new base instance.



看起来确实如此,但是我们可以在调用

派生的构造函数之前拦截它并以某种方式将它传递给新的基础而不是让b / b
比让CLR实例化一个新的基础。一些东西




public TaggedAnimal(Animal currentAnimal):base = currentAnimal

{


}


我不想使用反射,因为在一组对象上运行

会很昂贵

That does seem to be the case, but can we intercept it before the
derived constructor is called and pass it a new base somehow, rather
than letting the CLR instantiate a new base. Something along the lines
of

public TaggedAnimal(Animal currentAnimal) : base = currentAnimal
{

}

I would prefer not to use reflection as it will be expensive to run
over a collection of objects


-

Tom Porterfield
--
Tom Porterfield



ny********@hotmail.com 写道:

您好,

如何从

基类的实例创建派生类的实例,基本上包含现有的基类有一些

附加功能的课程。我需要这个的原因是因为我不是

总是能够控制/创建基础

类所具有的所有不同构造函数。我的问题可以在代码中描述如下......


/ *这是一个基本类,有一整堆

构造函数/功能* /

公共类动物

{

公共动物()

{/ ** /}

public Animal(int x)

{/ ** /}


}


/ *这是派生类,它为基础添加了一些功能* /

公共类TaggedAnimal:动物


{

公共字符串GetTag()

{/ ** /}


}


在我的主要方法中,我有一个完整的生成了

的Animal类的堆。我想将它们转换为TaggedAnimals并使用它们开始

。我不想只使用另一个方法/类来实现

给定Animal的GetTag()功能(使用动物作为成员
另一个类型的
) )因为有其他控件/类/方法

期望一个动物类型,我想将同一个对象传递给

它们。


所以主要代码看起来像这样:

公共类MyMainClass

{

public void MyMainMethod()

{

/ *动物在这里建造,我在这里没有多少控制权和

不要想要。* /

System.Collections.Arraylist allAnimals =

ThirdPartyORMTool.GetAllAnimals();


/ *但是一次我有实例,我想继承他们

来添加我自己的功能。 TagAllAnimals方法

强制转换/构造/创建动物实例到

TaggedAnimals * /

System.Collections.Arraylist allTaggedAnimals =

TagAllAnimals(allAnimals);


/ *现在我可以使用标记动物进行各种控制来

控制/显示任何功能在基础或派生类中* /

Gridview.DataSource = allTaggedAnimals;


/ *我还可以将同一个集合用于任何其他期望的人

一个Animal类(基类)* /

ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);

ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals); < br $> b
$ b}


}


我该怎么做呢?基本上,我的第一直觉就是做这样的事情。


公共类TaggedAnimal:动物


{


/ *将基类的实例传递给构造函数* /

public TaggedAnimal(Animal currentAnimal)

{

/ *将基类设置为基类的实例

传入* /

base = currentAnimal;

}


公共字符串GetTag()

{/ ** /}


}


但是在C#.net 2.0中,它抱怨base在这个

上下文中无效。


我可能会遗漏一些简单的东西,但是我如何设置/控制基类的

实例,而无需通过调用它来显式创建基础

类带有所需参数的构造函数。或者通过

创建派生类开始。


非常感谢任何帮助。
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don''t want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don''t have much control here and
don''t want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.



所以...如果我理解正确,你想在所有现有的类层次结构中添加一段

功能
你几乎没有(或没有控制权)?


我发誓这里有一个设计模式,但它让我感到很沮丧。 br />

任何人......?

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there''s a Design Pattern for that, but it slips my mind.

Anyone...?


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

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