English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
継承はオブジェクト指向プログラミングにおける最も重要な概念の1つです。継承はあるクラスに基づいて別のクラスを定義することを許可し、アプリケーションの作成と保守を容易にし、コードの再利用と開発時間の節約に寄与します。
クラスを作成する際には、プログラマーは完全に新しいデータメンバーやメンバ関数を再書き直す必要はなく、新しいクラスを設計し、すでにあるクラスのメンバを継承するだけで十分です。そのすでにあるクラスは基底クラス、この新しいクラスは派生類。
継承の考えが実現しました 属する(IS-A) 関係。例えば、哺乳類 属する(IS-A) 動物、犬 属する(IS-A) 哺乳類、したがって犬 属する(IS-A) 動物。
クラスは複数のクラスやインターフェースから派生することができ、これは複数の基底クラスやインターフェースからデータや関数を継承できることを意味します。
C# で派生クラスを作成する文法は以下の通りです:
<アクセス修飾子> class <基底クラス>
{
...
}
class <派生クラス> : <基底クラス>
{
...
}
例えば、ある基底クラス Shape が存在し、その派生クラスは Rectangle です:
using System;
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
派生クラスは基底クラスのメンバ変数とメンバメソッドを継承します。したがって、子クラスオブジェクトの作成前に親クラスオブジェクトが作成される必要があります。メンバ初期化リストで親クラスの初期化を行うことができます。
以下のプログラムはこの点を示しています:
using System;
namespace RectangleApplication
{
class Rectangle
{
// メンバ変数
protected double length;
protected double width;
public Rectangle(double l, double w41;
{
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 Tabletop40;double l, double w41; : base40;l, w41;
{125;
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
多重継承とは、一つのクラスが複数の親クラスから行動や特性を継承できることを指します。単一継承に対して、単一継承とは一つのクラスが一つの親クラスのみを継承できることを指します。
C# は多重継承をサポートしていません。しかし、インターフェースを使用して多重継承を実現できます。以下のプログラムはこの点を示しています:
using System;
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