f**********d 发帖数: 4960 | 1 if (subElement is InstrumentName)
InstrumentName = (InstrumentName)subElement;
都已经是InstrumentName了,为啥还要cast一下?
thx |
d****d 发帖数: 133 | 2 因为cast以后要调用InstrumentName的成员吧,这么写有点奇怪,一般都是用as,然后
check null,如果不是null就直接可以调用as出来的变量了。
【在 f**********d 的大作中提到】 : if (subElement is InstrumentName) : InstrumentName = (InstrumentName)subElement; : 都已经是InstrumentName了,为啥还要cast一下? : thx
|
a9 发帖数: 21638 | 3 不检查一下是不是InstrumentName直接就转会抛异常啊
【在 f**********d 的大作中提到】 : if (subElement is InstrumentName) : InstrumentName = (InstrumentName)subElement; : 都已经是InstrumentName了,为啥还要cast一下? : thx
|
c********1 发帖数: 5269 | 4 subElement could be declared as following
object subElement ;
you need to cast it to InstrumentName to access functions in InstrumentName.
【在 f**********d 的大作中提到】 : if (subElement is InstrumentName) : InstrumentName = (InstrumentName)subElement; : 都已经是InstrumentName了,为啥还要cast一下? : thx
|
c*********e 发帖数: 16335 | 5 if (subElement is InstrumentName)
instrumentName = (InstrumentName)subElement;
instrumentName是一個instance,InstrumentName是class名字吧?
【在 f**********d 的大作中提到】 : if (subElement is InstrumentName) : InstrumentName = (InstrumentName)subElement; : 都已经是InstrumentName了,为啥还要cast一下? : thx
|
c********1 发帖数: 5269 | 6 instrumentName starts with low case -- instance.
InstrumentName starts with upper case -- class name.
【在 c*********e 的大作中提到】 : if (subElement is InstrumentName) : instrumentName = (InstrumentName)subElement; : instrumentName是一個instance,InstrumentName是class名字吧?
|
B********r 发帖数: 397 | 7 这是脱裤子放屁吧,sub type到super type基本所有language都是implicit cast |
k**n 发帖数: 3989 | 8 用interface与继承时会出现这种需要cast的情况。
大概就下面这意思
1)
MyFunction(IMoveable aMoveableObject) {
if (aMoveableObject is ICar)
ICar myCar = (ICar)aMoveableObject;
...
}
也有人爱写成
2)
ICar myCar= aMoveableObject as ICar
if(myCar!=null){
..
}
代码1)比2)在各方面都好一点。 |
l**********n 发帖数: 8443 | |