【发布时间】:2013-11-18 11:44:24
【问题描述】:
我有一个接口,3 个实现该接口的类,其中一个类应该是抽象的。
iAnimal // Interface
Animal // Abstract Class (Implements iAnimal)
Fox // Class (Implements iAnimal)
Deer // Class (Implements iAnimal)
Animal animal; // declare an animal
Switch (type)
{
Case "Fox":
animal = new Fox();
break;
Case "Deer":
animal = new Deer();
break;
}
animal.eat();
我只想在动物上调用eat函数,这是switch语句的结果。
但是我得到了错误:
无法将类型 type1 隐式转换为 type2。
上面的逻辑有问题吗?
谢谢。
【问题讨论】:
-
你在哪一行得到这个错误?
type、type1、type2有哪些类型? -
嗯,如果您需要像这样的
switch语句,这通常意味着您在设计中做错了。
标签: c# class interface abstract