【发布时间】:2012-04-12 23:57:13
【问题描述】:
类:
interface ISms {
void f_SendSms();
}
class SmsVodafone : ISms {
public void f_SendSms(){
// ...
}
}
class SmsClickatell : ISms {
public void f_SendSms(){
// ...
}
}
这行得通:
ISms sms = null;
if (string.IsNullOrEmpty(_bilgi.M_Originator))
{
sms = new SmsVodafone();
}
else
{
sms = new SmsClickatell();
}
这也有效:
ISms sms = null;
sms = string.IsNullOrEmpty(_bilgi.M_Originator)
? (ISms) new SmsVodafone()
: new SmsClickatell();
这不起作用:
ISms sms = null;
sms = string.IsNullOrEmpty(_bilgi.M_Originator)
? new SmsVodafone()
: new SmsClickatell();
为什么?
【问题讨论】:
-
我假设它是 C# - 如果我错了,请更改标签
-
我认为在最后一个代码示例中,您打算删除
(ISms) -
我看不出你的第二个和第三个例子有什么区别?!
-
一定是错字,但我看不出第二个和第三个代码示例有什么区别
-
请参阅链接为重复的问题中的my answer。
标签: c# oop interface casting conditional-operator