[英]C# error CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler
I was trying the following code from a book when the error occured. 发生错误时,我正在尝试从书中的以下代码。 I am geussing it is generated because of wrong use of as keyword.
我在猜测它是由于错误使用as关键字而生成的。 Please, help me fix this error.
请帮我解决此错误。 This code is an example for subclassing.
此代码是子类化的示例。 This code generates two errors (cs0266).
此代码生成两个错误(cs0266)。 The error generating lines are in the Main method and are marked with a comment above the line.
错误生成行位于Main方法中,并在该行上方标有注释。
class Program
{
static void Main(string[] args)
{
// CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler
Rottweiler butch = new Rottweiler("Butch") as Dog;
// CS0266: Cannot implicitly convert type 'Dog' to 'Spaniel
Spaniel mac = new Spaniel("Mac", "yips") as Dog;
butch.Bark();
mac.Bark();
butch.DoYourThing();
mac.DoYourThing();
}
}
class Dog
{
protected string _name;
protected string _sound;
public Dog(string name)
{
_name = name; _sound = "barks";
}
public Dog(string name, string sound)
{
_name = name;
_sound = sound;
}
public void Bark()
{
Console.WriteLine("{0} {1} at you", _name, _sound);
}
public virtual void DoYourThing()
{
}
}
class Rottweiler : Dog
{
public Rottweiler(string name) : base(name) { }
public Rottweiler(string name, string sound) : base(name, sound) { }
public override void DoYourThing()
{
Console.WriteLine("{0} snarls at you, in a very menacing fashion!", _name);
}
}
class Spaniel : Dog
{
public Spaniel(string name) : base(name) { }
public Spaniel(string name, string sound) : base(name, sound) { }
public override void DoYourThing()
{
Console.WriteLine("{0} drools all over you, then licks you into submission!", _name);
}
}
While you can cast Spaniel
to Dog
, you can't do it the other way round. 虽然您可以将
Spaniel
投放到Dog
,但是却无法做到这一点。 So this code: 所以这段代码:
Spaniel mac = new Spaniel("Mac", "yips") as Dog;
Is casting to Dog
and then attempting to store that in a Spaniel
variable. 正在强制转换为
Dog
,然后尝试将其存储在Spaniel
变量中。 You can however do this: 但是,您可以这样做:
Dog mac = new Spaniel("Mac", "yips") as Dog;
Also as mentioned by @leppie, the as Dog
cast is not needed as there exists an implicit cast from a derived class to it's base: 就像@leppie提到的那样,由于存在从派生类到其基类的隐式转换,因此不需要
as Dog
强制转换:
Dog mac = new Spaniel("Mac", "yips");
Well, here: Rottweiler butch = new Rottweiler("Butch") as Dog;
好吧,这里:
Rottweiler butch = new Rottweiler("Butch") as Dog;
you're creating instance of Rottweiler
and casting it to the Dog
. 您正在创建
Rottweiler
实例并将其投放到Dog
。
Still ok for now, but then you're assigning instance of Dog
to the variable of type Rottweiler
- but this is impossible. 现在还是可以的,但是您要将
Dog
实例分配给Rottweiler
类型的变量-但这是不可能的。
Since Rottweiler
is inherited from Dog
, every Rottweiler
is Dog
, but not every Dog
is Rottweiler
- thus this implicit cast during assignment can't be done. 由于
Rottweiler
是从Dog
继承的,因此每个Rottweiler
都是Dog
,但是并不是每个Dog
都是Rottweiler
,因此分配期间无法执行此隐式转换。
So either remove cast to Dog
所以,要么删除投给
Dog
Rottweiler butch = new Rottweiler("Butch");
or change type of butch
to Dog
: 或将
butch
类型更改为Dog
:
Dog butch = new Rottweiler("Butch");
Notice in this case you also don't need explicit cast ... as Dog
, it will be done implicitly with assignment; 请注意,在这种情况下,您也不需要显式的强制转换
... as Dog
,它将通过赋值隐式完成;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.