基类方法可以返回继承类typ的对象 [英] Can a base class method return an object of an inherited class typ

查看:77
本文介绍了基类方法可以返回继承类typ的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个类BiologySequence看起来像这样。


公共课BiologySequence

{

私人字符串_Sequence;


公共字符串序列

{

get

{

return _Sequence;

}

套装

{

_Sequence = value;

}

}


公共BiologySequence(字符串序列)

{

if(sequence!= null)

{

_Sequence = sequence;

}

else

{

_Sequence = string.Empty;

}

}

}


和一个继承的类( NucleicAcidSequence)没有其他属性

对这个问题很重要。


我希望基类有一个子序列

的方法与string的substring方法基本相同。我写了


public BiologySequence Subsequence(int firstBase,int lastBase)

{

BiologySequence ToReturn = this.MemberwiseClone() ;

ToReturn.Sequence = _Sequence.Substring(firstBase,lastBase -

firstBase);

return(ToReturn);

}

,我认为有效,但它总是返回一个类型为

BiologySequence的对象,无论它是从哪个实际类调用。


我已经尝试了各种各样的东西来重铸要返回的对象,因为

与调用类型相同,但我找不到一个有效的。 br />
有没有办法做到这一点,或者我是否需要为每个继承类的

显式编写这个方法?


谢谢!

Ethan


Ethan Strauss博士

生物信息学科学家

Promega Corporation

2800 Woods Hollow Rd。

麦迪逊,WI 53711

608-274-4330

800-356-9526
et *********** @ promega .com

推荐答案

Ethan,


鉴于约束系统的局限性(你无法
调用NucleicAcidSequence的构造函数,因为唯一的构造函数

你可以创建一个约束对象是默认的无参数的一个),你

应该是一个无参数构造函数,然后使你的

子序列方法通用:

public T Subsequence< T>(int firstBase ,int lastBase)其中T:

BiologySequence,new()

{

//创建返回值。

T retVal = new T();


//分配新序列。

retVal.Sequence = Sequence.Substring(firstBase,lastBase - firstBase );


//返回。

返回retVal;

}


当然,这意味着任何事情派生自BiologySequence必须

有一个默认的无参数构造函数,如果他们想使用子序列

方法。如果你有其他属性,他们也必须被复制。

这里的问题是,如果你有一个大的层次链,​​那么你将需要b
$ b很多案例陈述将复制所有适用的

属性,或者使用反射,这可能不是你想要首先想到的方向。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Ethan Strauss" < Et ********** @ discussion.microsoft.com写信息

新闻:AA ****************** **************** @ microsof t.com ...
Ethan,

Given the limitations of the constraint system (you won''t be able to
call the constructor of the NucleicAcidSequence, since the only constructor
you can create a constraint against is the default parameterless one), you
should probably make a parameterless constructor, and then make your
Subsequence method generic:

public T Subsequence<T>(int firstBase, int lastBase) where T :
BiologySequence, new()
{
// Create the return value.
T retVal = new T();

// Assign the new sequence.
retVal.Sequence = Sequence.Substring(firstBase, lastBase - firstBase);

// Return.
return retVal;
}

Of course, it means that anything that derives from BiologySequence must
have a default parameterless constructor if they want to use the Subsequence
method. If you have other properties, they have to be copied over as well.
The problem here is that if you have a large hierarchy chain, then you will
have to have a lot of case statements which will copy all the applicable
properties, or use reflection, which is probably not the direction you
wanted to head in the first place.

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

"Ethan Strauss" <Et**********@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...




我有一个类BiologySequence看起来像这样。


公共课BiologySequence

{

私人字符串_Sequence;


公共字符串序列

{

get

{

return _Sequence;

}

套装

{

_Sequence = value;

}

}


公共BiologySequence(字符串序列)

{

if(sequence!= null)

{

_Sequence = sequence;

}

else

{

_Sequence = string.Empty;

}

}

}


和一个继承的类( NucleicAcidSequence)没有其他属性

对这个问题很重要。


我希望基类有一个子序列

的方法与string的substring方法基本相同。我写了


public BiologySequence Subsequence(int firstBase,int lastBase)

{

BiologySequence ToReturn = this.MemberwiseClone() ;

ToReturn.Sequence = _Sequence.Substring(firstBase,lastBase -

firstBase);

return(ToReturn);

}

,我认为有效,但它总是返回一个类型为

BiologySequence的对象,无论它是从哪个实际类调用。


我已经尝试了各种各样的东西来重铸要返回的对象



与调用类型相同的类型,但我可以'找不到一个有用的。

有没有办法做到这一点,或者我需要明确地写这个方法

for

每个继承课程?


谢谢!

Ethan


Ethan Strauss博士

生物信息学科学家

Promega公司

2800伍兹空心路。

麦迪逊,WI 53711

608-274-4330

800-356-9526
et *********** @ promega.com



2008年1月9日星期三12:39:02 -0800,Ethan Strauss

< Et ********** @ discussion.microsoft .comwrote:
On Wed, 09 Jan 2008 12:39:02 -0800, Ethan Strauss
<Et**********@discussions.microsoft.comwrote:

[...]

我希望基类有一个子序列

的方法与string的substring方法基本相同。我写了


public BiologySequence Subsequence(int firstBase,int lastBase)

{

BiologySequence ToReturn = this.MemberwiseClone() ;

ToReturn.Sequence = _Sequence.Substring(firstBase,lastBase -

firstBase);

return(ToReturn);

}

,我认为有效,但它总是返回一个类型为

BiologySequence的对象,无论它是从哪个实际类调用。


我已经尝试了各种各样的东西来重铸要返回的对象



与调用类型相同的类型,但我可以'找不到一个有用的。

有没有办法做到这一点,或者我需要明确地写这个方法

for

每个继承课程?
[...]
I want to have the base class have a "Subsquence" method which is
essentially the same as the substring method of string. I have written

public BiologySequence Subsequence(int firstBase, int lastBase)
{
BiologySequence ToReturn = this.MemberwiseClone();
ToReturn.Sequence = _Sequence.Substring(firstBase, lastBase -
firstBase);
return (ToReturn);
}
which, I think works, but it always returns an object of type
BiologySequence regardless of what actual class it is called from.

I have tried all sorts of things to recast the object to be returned as
the
same type as the calling type, but I can''t find one which works.
Is there a way to do this, or do I need to explicitly write this method
for
each inheriting class?



你能否提供一个完整但简洁的代码示例,这些代码写成

你希望它可以工作,但是这不是吗?


Object.MemberwiseClone()方法将返回与正在使用的

实例相同的类型。所以你发布的代码应该完全按照你想做的那样来做我喜欢的事情。如果您的实例实际上是一个

NucleicAcidSequence,那么在该实例上调用MemberwiseClone()

应该返回一个NucleicAcidSequence,即使从
$中的方法调用b $ b BiologySequence。


该方法将返回一个类型为BiologySequence的引用,但

调用者应该能够毫无困难地转换为NucleicAcidSequence。


这是一个简单的程序,演示了这个工作:


使用System;

使用System.Collections .Generic;

使用System.Text;


命名空间TestCloneBaseClass

{

class Program

{

A类:ICloneable

{

#region ICloneable会员


公共对象克隆()

{

返回this.MemberwiseClone();

}


#endregion

}


B级:A

{

}


stat ic void Main(string [] args)

{

A a = new B();

B b =(B)a。克隆();


Console.WriteLine(" b的类型:" + b.GetType()。姓名);

Console.ReadLine();

}

}

}

Can you provide a complete-but-concise example of code that is written as
you''d like it to work, but which doesn''t?

The Object.MemberwiseClone() method will return the same type as the
instance being used. So the code you''ve posted should do exactly what it
_seems_ like to me that you want to do. If your instance is actually a
NucleicAcidSequence, then calling MemberwiseClone() on that instance
should return you a NucleicAcidSequence, even when called from a method in
BiologySequence.

The method will return a reference typed as BiologySequence, but the
caller should be able to cast to NucleicAcidSequence without any trouble..

Here''s a simple program that demonstrates this working:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestCloneBaseClass
{
class Program
{
class A : ICloneable
{
#region ICloneable Members

public object Clone()
{
return this.MemberwiseClone();
}

#endregion
}

class B : A
{
}

static void Main(string[] args)
{
A a = new B();
B b = (B)a.Clone();

Console.WriteLine("Type of b: " + b.GetType().Name);
Console.ReadLine();
}
}
}


谢谢Peter。

我无法编译。

我得到了不能将类型''对象'隐式转换为

''BiologyTools.BiologySequence''。存在显式转换(你是否b
缺少一个演员?)"在包含MemberwiseClone的行上。


Ethan

" Peter Duniho"写道:
Thanks Peter.
I can''t get it to compile.
I get "Cannot implicitly convert type ''object'' to
''BiologyTools.BiologySequence''. An explicit conversion exists (are you
missing a cast?)" on the line which contains MemberwiseClone.

Ethan
"Peter Duniho" wrote:

2008年1月9日星期三12:39:02 -0800,Ethan Strauss

< Et ***** *****@discussions.microsoft.comwrote:
On Wed, 09 Jan 2008 12:39:02 -0800, Ethan Strauss
<Et**********@discussions.microsoft.comwrote:

[...]

我希望基类有一个" Subsquence"

的方法与string的substring方法基本相同。我写了


public BiologySequence Subsequence(int firstBase,int lastBase)

{

BiologySequence ToReturn = this.MemberwiseClone() ;

ToReturn.Sequence = _Sequence.Substring(firstBase,lastBase -

firstBase);

return(ToReturn);

}

,我认为有效,但它总是返回一个类型为

BiologySequence的对象,无论它是从哪个实际类调用。


我已经尝试了各种各样的东西来重铸要返回的对象



与调用类型相同的类型,但我可以'找不到一个有用的。

有没有办法做到这一点,或者我需要明确地写这个方法

for

每个继承课程?
[...]
I want to have the base class have a "Subsquence" method which is
essentially the same as the substring method of string. I have written

public BiologySequence Subsequence(int firstBase, int lastBase)
{
BiologySequence ToReturn = this.MemberwiseClone();
ToReturn.Sequence = _Sequence.Substring(firstBase, lastBase -
firstBase);
return (ToReturn);
}
which, I think works, but it always returns an object of type
BiologySequence regardless of what actual class it is called from.

I have tried all sorts of things to recast the object to be returned as
the
same type as the calling type, but I can''t find one which works.
Is there a way to do this, or do I need to explicitly write this method
for
each inheriting class?



你能否提供一个完整而简洁的代码示例,这些代码写成

你希望它可以工作,但是这不是吗?


Object.MemberwiseClone()方法将返回与正在使用的

实例相同的类型。所以你发布的代码应该完全按照你想做的那样来做我喜欢的事情。如果您的实例实际上是一个

NucleicAcidSequence,那么在该实例上调用MemberwiseClone()

应该返回一个NucleicAcidSequence,即使从
$中的方法调用b $ b BiologySequence。


该方法将返回一个类型为BiologySequence的引用,但

调用者应该能够毫无困难地转换为NucleicAcidSequence。


这是一个简单的程序,演示了这个工作:


使用System;

使用System.Collections .Generic;

使用System.Text;


命名空间TestCloneBaseClass

{

class Program

{

A类:ICloneable

{

#region ICloneable会员


公共对象克隆()

{

返回this.MemberwiseClone();

}


#endr egion

}


B级:A

{

}


static void Main(string [] args)

{

A a = new B();

B b =(B)a.Clone();


Console.WriteLine(" b的类型:" + b.GetType()。姓名);

Console.ReadLine();

}

}

}


Can you provide a complete-but-concise example of code that is written as
you''d like it to work, but which doesn''t?

The Object.MemberwiseClone() method will return the same type as the
instance being used. So the code you''ve posted should do exactly what it
_seems_ like to me that you want to do. If your instance is actually a
NucleicAcidSequence, then calling MemberwiseClone() on that instance
should return you a NucleicAcidSequence, even when called from a method in
BiologySequence.

The method will return a reference typed as BiologySequence, but the
caller should be able to cast to NucleicAcidSequence without any trouble..

Here''s a simple program that demonstrates this working:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestCloneBaseClass
{
class Program
{
class A : ICloneable
{
#region ICloneable Members

public object Clone()
{
return this.MemberwiseClone();
}

#endregion
}

class B : A
{
}

static void Main(string[] args)
{
A a = new B();
B b = (B)a.Clone();

Console.WriteLine("Type of b: " + b.GetType().Name);
Console.ReadLine();
}
}
}


这篇关于基类方法可以返回继承类typ的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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