// interface
public interface IHasLegs { ... }
// base class
public class Animal { ... }
// derived classes of Animal
public class Donkey : Animal, IHasLegs { ... } // with legs
public class Lizard : Animal, IHasLegs { ... } // with legs
public class Snake : Animal { ... } // without legs
// other class with legs
public class Table : IHasLegs { ... }
public class CageWithAnimalsWithLegs {
public List<??> animalsWithLegs { get; set; }
}
??には何を入れればいいですか?Animal
とIHasLegs
?の両方から継承するオブジェクトを強制するには Snake
あの檻の中の も も見たくないTable
。
- - - - - - - 編集 - - - - - - -
皆さんの回答に感謝しますが、ここに問題があります:私が実際にやりたいことはこれです:
public interface IClonable { ... }
public class MyTextBox : TextBox, IClonable { ... }
public class MyComboBox : ComboBox, IClonable { ... }
TextBox/ComboBox はもちろん Control です。ここで、Control と IClonable の両方を継承する抽象クラスを作成すると、必要な TextBox/ComboBox の継承が失われます。複数のクラスの継承は許可されていないため、インターフェイスを操作する必要があります。もう一度考えてみると、IClonable から継承する別のインターフェイスを作成できます。
public interface IClonableControl : IClonable { ... }
public class MyTextBox : TextBox, IClonableControl { ... }
public class MyComboBox : ComboBox, IClonableControl { ... }
その後
List<IClonableControl> clonableControls;
ありがとうございました!!