我有以下内容。
只有当变量在构造函数中时,编译器才会识别派生类中的变量 flyBehaviour。这是为什么?
abstract class Duck
{
protected IFlyBehaviour flyBehaviour;
public IFlyBehaviour FlyBehaviour
{
get
{return flyBehaviour;}
set
{flyBehaviour=value;}
}
}
class MullardDuck: Duck
{
flyBehaviour //the compiler doesn't recognize this variable here
public MullardDuck()
{
flyBehaviour = new FlyWithWings(); //here the compiler recognize this variable
}
}