抽象化
メニューを表示するにはスワイプしてください
抽象化は、オブジェクト指向プログラミング(OOP)における基本的な概念であり、複雑な実装の詳細を隠し、本質的な機能に集中することを可能にする手法。C#では、抽象化は抽象クラスを通じて実現される。
OOPは当初、カプセル化、継承、ポリモーフィズムの3つの原則(「OOPの三本柱」とも呼ばれる)に基づいていた。そのため、抽象化は新たに加えられた概念であり、一部の資料では基本概念と見なされない場合もあるが、重要な概念であることに変わりはない。
抽象クラスはインスタンス化できないクラスであり、そのクラスのオブジェクトを作成することはできない。抽象クラスは他のクラスと同様に属性やメソッドを持つことができるが、抽象メソッド(設計図となるメソッド)を含むこともでき、これらは派生クラスによって実装されることを意図している。
abstractキーワードをクラス定義の前に付けることで抽象クラスを作成できる。例えば、Shapeという抽象クラスを作成する例:
index.cs
12345678910111213141516171819using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }
同様に、abstract キーワードを戻り値の型の前に追加することで、抽象メソッド を作成可能。
抽象メソッド には本体がなく、設計図のみを提供。
index.cs
1234567891011abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }
これの目的は、他のクラスのための設計図を作成すること。これにより、コードの簡素化が可能。これをよりよく理解するために、第5章のポリモーフィズムの課題を見てみる:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }
上記の例では、Shape クラスを実際に使用することは想定していないが、それでも getArea クラス内で calculatePerimeter および Shape メソッドのダミー実装を書く必要があった。このコードは、Shape クラスを抽象クラスにすることで多少簡素化できる。また、getArea と calculatePerimeter メソッドも抽象メソッドにできる。
メソッドは abstract または virtual のいずれか一方のみ指定可能で、同時に両方を指定することはできません。ただし、abstract メソッドは基本的に virtual(オーバーライド可能)メソッドですが、基底クラスには本体が存在しません。
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100using System; using System.Collections.Generic; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
1. クラスを抽象クラスにするために使用されるキーワードはどれですか?
2. 抽象クラスはインスタンス化できますか?
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください