Code Fix

上級sealedメンバーをオーバーライドしようとした原因と直し方 › パターン3

sealedメンバーをオーバーライドしようとした原因と直し方

sealed overrideを付けたメンバーを、さらに下の派生クラスでオーバーライドしようとすると出るコンパイルエラーです。

 1  class Shape {
 2      public virtual double Area() { return 0; }
 3      public virtual string Name() { return "shape"; }
 4  }
 5  class Square : Shape {
 6      public sealed override double Area() { return 4; }
 7      public override string Name() { return "square"; }
 8  }
 9  class SpecialSquare : Square {
10      public override ___________________________________
                                         ^
11  }
12  class Program {
13      static void Main(string[] args) {
14          SpecialSquare s = new SpecialSquare();
15          System.Console.WriteLine(s.Area());
16          System.Console.WriteLine(s.Name());
17      }
18  }
Program.cs(10,28): error CS0239: 'SpecialSquare.Area()': cannot override inherited member 'Square.Area()' because it is sealed

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

sealedメンバーをオーバーライドしようとした原因と直し方

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)