从基础对象创建派生对象 [英] Creating a derived object from a base object

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

问题描述

是否有一种简单/特殊的方法可以将基础对象转换为派生的

对象?例如,给出以下内容:


class MyString:String

{

....
< br $>
}

(好吧,所以字符串是密封的,但只是一起玩)


MyString myString = new MyString();

说我想将myString转换为大写 - 确实很好用

使用基本String对象的ToUpper方法 - 但似乎不是

就像我可以:


myString.ToUpper()返回一个字符串,而不是一个MyString


那我怎么能用ToUpper ()如果我需要结果是MyString?我不能将结果转换为MyString,因为我不能将基类型转换为


派生类型。返回新String的所有String方法现在都没用吗?


我想,更简单的说,我的问题是什么是最简单的方法

将基类型转换为派生类型?如何将字符串转换为

MyString?鉴于MyString是一个String类型,是否有任何特殊的

方法将String转换为MyString?或者现在它完全是2个完全不同的对象,就像String和Foo对象一样。我需要

来编写一个显式的转换方法来进行转换吗?


谢谢

解决方案

创建后永远不能更改对象的类型。但是,您可以提供隐式或显式转换运算符,这些运算符可以在两种

类型之间进行转换,并且您可以提供new类型。更改

返回类型的方法的实现,但是应该谨慎使用,因为任何通过基类引用使用

子类对象的调用者都将使用* old *方法。


以下显示了使用虚拟的方法。和新的到处走走的方法

这个;在基类实例上调用总是返回一个基类;调用一个

子类实例总是返回一个子类 - 但请注意它是通过引用键入

- 所以如果调用者将它引用为BaseClass,那么它将会看到一个基类,并且如果它将它作为子类引用它的话。它会看到一个

子类。无论哪种方式,实例仍然是SubClass。 new

方法中的强制转换允许进一步的子类继续覆盖InnerSomething。


Marc


公共类BaseClass {

public BaseClass Something(){

返回InnerSomething();

}


protected virtual BaseClass InnerSomething(){

返回新的BaseClass();

}

}


公共类SubClass:BaseClass {

protected override BaseClass InnerSomething(){

返回新的SubClass();

}


public new SubClass Something(){

return(SubClass)InnerSomething();

}

}


一个解决方案就是这样做,假设你有一个构造函数

取一个字符串,这似乎是合理的:


MyString mystring = new MyString(" a case case string");

mystring = new MyString(mystring.ToUpper());


HTH

Peter

" benliu" < be **** @ iasportsmedia.comWrote in message

news:11 ********************** @ j44g2000cwa.googlegr oups.com ...


是否有一种简单/特殊的方法可以将基础对象转换为派生的

对象?例如,给出以下内容:


class MyString:String

{

...


}


(好吧,所以字符串是密封的,但只是一起玩)


MyString myString = new MyString( );


说我想将myString转换为大写 - 确实很好用

使用基本String对象的ToUpper方法 - 但不是'好像

就像我可以:


myString.ToUpper()返回一个字符串,而不是一个MyString


那么如果我需要将结果作为MyString,我如何使用ToUpper()?我不能将结果转换为MyString,因为我不能将基类型转换为


派生类型。返回新String的所有String方法现在都没用吗?


我想,更简单的说,我的问题是什么是最简单的方法

将基类型转换为派生类型?如何将字符串转换为

MyString?鉴于MyString是一个String类型,是否有任何特殊的

方法将String转换为MyString?或者现在它完全是2个完全不同的对象,就像String和Foo对象一样。我需要

来编写一个显式的转换方法来进行转换吗?


谢谢



好的,所以你建议的解决方案是我遇到的真正问题。


你怎么写一个带字符串的MyString构造函数?你

没有访问String的底层实现(如果你能想到一个hacky方式,甚至

,这只是一个例子,让' 's
假设String类是黑盒子)。那么构造函数如何从String的实例创建自己怎么样?


所以现在这与你正在尝试的情况不同从一个完全不相关的对象创建一个

对象?似乎因为MyString

没有向String添加任何新属性,所以应该有一个快速简单的方法来从String对象构造MyString对象。也许不是吗?

Peter Bradley写道:


发生的一个解决方案就是这样做,假设你有一个构造函数

需要一个字符串,这似乎是合理的:


MyString mystring = new MyString(" a case case string);

mystring = new MyString(mystring.ToUpper());


HTH


Peter


" benliu" ; < be **** @ iasportsmedia.comWrote in message

news:11 ********************** @ j44g2000cwa.googlegr oups.com ...


是否有一种简单/特殊的方法可以将基础对象转换为派生的

对象?例如,给出以下内容:


class MyString:String

{

...


}

(好吧,所以字符串是密封的,但只是一起玩)


MyString myString = new MyString();

说我想将myString转换为大写 - 确实很好用

使用基本String对象的ToUpper方法 - 但似乎并不是很好b $ b就像我可以:


myString.ToUpper()返回一个字符串,而不是一个MyString


那我该如何使用ToUpper( )如果我需要结果是MyString?我不能将结果转换为MyString,因为我不能将基类型转换为


派生类型。返回新String的所有String方法现在都没用吗?


我想,更简单的说,我的问题是什么是最简单的方法

将基类型转换为派生类型?如何将字符串转换为

MyString?鉴于MyString是一个String类型,是否有任何特殊的

方法将String转换为MyString?或者现在它完全是2个完全不同的对象,就像String和Foo对象一样。我需要

来编写一个显式的转换方法来进行转换?


谢谢


Is there an easy/special way to turn a base object into a derived
object? So for example, given the following:

class MyString : String
{
....

}
(ok, so String is sealed, but just play along)

MyString myString = new MyString();
say i want to convert myString to upper case - sure would be nice to
use the ToUpper method of the base String object - but doesn''t seem
like i can:

myString.ToUpper() returns a String, not a MyString

So how can i use ToUpper() if i need the result to be a MyString? I
can''t cast the result to a MyString since I can''t cast a base type to a

derived type. Are all the String methods that return a new String
instance useless now?

I guess, even more simply, my question is what is the easiest way to
convert a base type to a derived type? How do i convert a String to a
MyString? Given that MyString is a String type, is there any special
way to convert String into MyString? Or is it literally 2 completely
different objects now, just like a String and a Foo Object. And i need
to write a explicit cast method to do the conversion?

Thanks

解决方案

You can never change the type of an object once created. You can however
provide implicit or explicit cast operators that convert between the two
types, and you can provide "new" implementations of methods to change the
return types, but this should be used with caution as any callers using a
subclass object thru a baseclass reference will use the *old* methods.

The following shows a way to use "virtual" and "new" methods to get around
this; called on a baseclass instance always returns a baseclass; called on a
subclass instance always returns a subclass - however note that it is typed
by the reference - so if the caller is referencing it as "BaseClass" it will
see a baseclass, and it if is referencing it as a "SubClass" it will see a
subclass. Either way the instance is still a SubClass. The cast in the "new"
method allows further subclasses to continue to override InnerSomething.

Marc

public class BaseClass {
public BaseClass Something() {
return InnerSomething();
}

protected virtual BaseClass InnerSomething() {
return new BaseClass();
}
}

public class SubClass : BaseClass {
protected override BaseClass InnerSomething() {
return new SubClass();
}

public new SubClass Something() {
return (SubClass) InnerSomething();
}
}


One solution that occurs is to do this, assuming you have a constructor that
takes a string, which seems reasonable:

MyString mystring = new MyString("a lower case string");
mystring = new MyString(mystring.ToUpper());

HTH
Peter
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...

Is there an easy/special way to turn a base object into a derived
object? So for example, given the following:

class MyString : String
{
...

}
(ok, so String is sealed, but just play along)

MyString myString = new MyString();
say i want to convert myString to upper case - sure would be nice to
use the ToUpper method of the base String object - but doesn''t seem
like i can:

myString.ToUpper() returns a String, not a MyString

So how can i use ToUpper() if i need the result to be a MyString? I
can''t cast the result to a MyString since I can''t cast a base type to a

derived type. Are all the String methods that return a new String
instance useless now?

I guess, even more simply, my question is what is the easiest way to
convert a base type to a derived type? How do i convert a String to a
MyString? Given that MyString is a String type, is there any special
way to convert String into MyString? Or is it literally 2 completely
different objects now, just like a String and a Foo Object. And i need
to write a explicit cast method to do the conversion?

Thanks



Ok, so your suggested solution is the real issue i''m getting at.

How WOULD you write a MyString constructor that takes a string? You
don''t have access to the underlying implementation of String (and even
if you can think of a hacky way, this is just an example, and let''s
assume that the String class is a black box). So therefore how would
the constructor create itself from an instance of a String?

So is this now different than in a case that you''re trying to create an
object from a completely unrelated object? Seems that since MyString
does not add any new properties to String, there should be a quick easy
way to construct a MyString object from a String object. Maybe not?
Peter Bradley wrote:

One solution that occurs is to do this, assuming you have a constructor that
takes a string, which seems reasonable:

MyString mystring = new MyString("a lower case string");
mystring = new MyString(mystring.ToUpper());

HTH
Peter
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...

Is there an easy/special way to turn a base object into a derived
object? So for example, given the following:

class MyString : String
{
...

}
(ok, so String is sealed, but just play along)

MyString myString = new MyString();
say i want to convert myString to upper case - sure would be nice to
use the ToUpper method of the base String object - but doesn''t seem
like i can:

myString.ToUpper() returns a String, not a MyString

So how can i use ToUpper() if i need the result to be a MyString? I
can''t cast the result to a MyString since I can''t cast a base type to a

derived type. Are all the String methods that return a new String
instance useless now?

I guess, even more simply, my question is what is the easiest way to
convert a base type to a derived type? How do i convert a String to a
MyString? Given that MyString is a String type, is there any special
way to convert String into MyString? Or is it literally 2 completely
different objects now, just like a String and a Foo Object. And i need
to write a explicit cast method to do the conversion?

Thanks


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

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