由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个代码啥意思c#
相关主题
再一个问题c++为什么C++每次产生的随机数都是一样的
请教char *和char []的判断纯虚函数问题
发现一个有趣的现象。怎么可以做到程序运行时编译。
请教各路C++大神 为什么f(3) 输出是 'dd'请教一个php问题
[合集] 为什么很少有人用static来实现signleton?怎么在两个函数见找code path
A question about singleton[bssd]通天塔图书馆
大家每年给公司check in大概多少行的代码?这段代码为何要这样做?
请教一个程序调用的内存问题 (转载)谁知道这个问题的答案 (转载)
相关话题的讨论汇总
话题: subelement话题: cast话题: icar
进入Programming版参与讨论
1 (共1页)
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
9
这么初级的问题竟然有人问
1 (共1页)
进入Programming版参与讨论
相关主题
谁知道这个问题的答案 (转载)[合集] 为什么很少有人用static来实现signleton?
TryGetValue (c#)A question about singleton
请问一个implicit conversion的问题(C++)大家每年给公司check in大概多少行的代码?
one question about initializaiton list请教一个程序调用的内存问题 (转载)
再一个问题c++为什么C++每次产生的随机数都是一样的
请教char *和char []的判断纯虚函数问题
发现一个有趣的现象。怎么可以做到程序运行时编译。
请教各路C++大神 为什么f(3) 输出是 'dd'请教一个php问题
相关话题的讨论汇总
话题: subelement话题: cast话题: icar