これを修正する方法はありません。あなたのクラスfoo
は正しいです。エラーメッセージはそれをすべて説明しています。foo
継承しませんPictureBox
。foo
がある種のピクチャ ボックスである場合は、 ではPictureBox
なくクラスを実装しControl
ます。
実際の例を挙げると:
interface IAnimal { }
class Dog : IAnimal { public static void Bark() { } }
class Cat : IAnimal { public static void Meow() { } }
の署名はas defined とCat
は異なりますが、そうではありません。を定義しますが、定義しません。たとえば、コメント付きの次のコードは、これを理解するのに役立ちます。Dog
Cat
Cat.Meow()
Dog
Dog
Dog.Bark()
Cat
class Program
{
static void Main(string[] args)
{
Dog myDog = new Dog(); // myDog contains definition for Bark
IAnimal myPet = (IAnimal)myDog; // Cast not necessary.
// myPet's signiature is of Dog, but typeof(myPet)
// is Animal as it was boxed (google for help on this)
Cat myCat = (Cat)myPet // now try and imagine this in real life
// (turning a dog into a cat) [I don't even]
// It doesn't work because Cat doesn't
// contain a definition for Bark()
Cat myDimentedCat = (Cat)(IAnimal)myDog; // which is why this code fails.
}
}
私が見せようとしているのは、次のものと同じです
a square is a rectangle, but a rectangle isn't always a square
:
interface IRectangle { }
interface ISquare : IRectangle { }