C# 継承
継承はオブジェクト指向プログラミングにおける最も重要な概念の一つです。継承により、あるクラスに基づいて別のクラスを定義できるため、アプリケーションの作成と保守が容易になります。同時に、コードの再利用と開発時間の短縮にも役立ちます。
クラスを作成するとき、プログラマーは新しいデータメンバーとメンバー関数を完全に書き直す必要はなく、既存のクラスのメンバーを継承した新しいクラスを設計するだけで済みます。この既存のクラスは基底クラスと呼ばれ、この新しいクラスは派生クラス。
と呼ばれます。継承の考え方は〜である(IS-A)関係を実現します。例えば、哺乳類は〜である(IS-A)動物であり、犬は〜である(IS-A)哺乳類です。したがって、犬は〜である(IS-A)動物です。
基底クラスと派生クラス
あるクラスは別のクラスから継承でき、基底クラス(親クラス)と派生クラス(子クラス)と呼ばれます。
C# はクラスの多重継承をサポートしませんが、インターフェースの多重継承はサポートしており、1つのクラスで複数のインターフェースを実装できます。
要約すると:1つのクラスは複数のインターフェースを継承できますが、継承できるクラスは1つだけです。
C# で派生クラスを作成する構文は次のとおりです:
{
...
}
class <派生クラス> : <基底クラス>
{
...
}
派生クラスは、明示的に private(プライベート)とマークされていない限り、基底クラスのメンバー(フィールド、メソッド、プロパティなど)を継承します。
派生クラスはキーワード base を使用して基底クラスのコンストラクターとメソッドを呼び出すことができます。
例
{
public void SomeMethod()
{
// Method implementation
}
}
class DerivedClass : BaseClass
{
public void AnotherMethod()
{
// Accessing base class method
base.SomeMethod();
// Method implementation
}
}
たとえば、基底クラス Shape があり、その派生クラスが Rectangle であると仮定します:
例
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// 派生クラス
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// オブジェクトの面積を出力
Console.WriteLine("総面積: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
上記のコードがコンパイルおよび実行されると、次の結果が生成されます:
总面积: 35
基底クラスの初期化
派生クラスは基底クラスのメンバー変数とメンバーメソッドを継承します。したがって、親クラスのオブジェクトは子クラスのオブジェクトが作成される前に作成される必要があります。メンバー初期化リストで親クラスの初期化を行うことができます。
次のプログラムはこれを示しています:
例
namespace RectangleApplication
{
class Rectangle
{
// メンバー変数
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("長さ: {0}", length);
Console.WriteLine("幅: {0}", width);
Console.WriteLine("面積: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("コスト: {0}", GetCost());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
上記のコードがコンパイルおよび実行されると、次の結果が生成されます:
长度: 4.5 宽度: 7.5 面积: 33.75 成本: 2362.5
インターフェースの継承(Interface Inheritance)
1つのインターフェースは1つ以上の他のインターフェースから継承でき、派生インターフェースは基底インターフェースのすべてのメンバーを継承します。
派生インターフェースは基底インターフェースのメンバーリストを拡張できますが、それらのアクセス修飾子を変更することはできません。
例
{
void Method1();
}
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
インターフェースを継承するインスタンスは、次の方法で実装できます:
例
// 基底インターフェースを定義
interface IBaseInterface
{
void Method1();
}
// 基底インターフェースから継承する派生インターフェースを定義
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
// 派生インターフェースを実装するクラス
class MyClass : IDerivedInterface
{
public void Method1()
{
Console.WriteLine("Method1 implementation");
}
public void Method2()
{
Console.WriteLine("Method2 implementation");
}
}
class Program
{
static void Main(string[] args)
{
// MyClass クラスのインスタンスを作成
MyClass obj = new MyClass();
// 基底インターフェースから継承したメソッドを呼び出す
obj.Method1();
// 派生インターフェースで追加されたメソッドを呼び出す
obj.Method2();
}
}
上記の例では、MyClass クラスは IDerivedInterface インターフェースを実装しているため、IBaseInterface から継承した Method1() メソッドを含む、IDerivedInterface で定義されたすべてのメソッドを提供する必要があります。Main メソッドでは、MyClass のインスタンス obj を作成し、そのメソッドを呼び出しました。
出力結果は次のとおりです:
Method1 implementation Method2 implementation
C# 多重継承
多重継承とは、1つのクラスが複数の親クラスから同時に動作と特性を継承できる機能のことです。単一継承(1つのクラスが1つの親クラスからのみ継承できること)とは対照的です。
C# は多重継承をサポートしていません。ただし、インターフェースを使用して多重継承を実現できます。次のプログラムはこれを示しています:
例
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// 基底クラス PaintCost
public interface PaintCost
{
int getCost(int area);
}
// 派生クラス
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// オブジェクトの面積を出力
Console.WriteLine("総面積: {0}", Rect.getArea());
Console.WriteLine("塗装総コスト: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
上記のコードがコンパイルおよび実行されると、次の結果が生成されます:
总面积: 35 油漆总成本: $2450その他の拡張