Csharp-inheritance
提供:Dev Guides
C#-継承
オブジェクト指向プログラミングで最も重要な概念の1つは継承です。 継承を使用すると、クラスを別のクラスの観点から定義でき、アプリケーションの作成と保守が簡単になります。 これは、コード機能を再利用する機会を提供し、実装時間を短縮します。
クラスを作成するとき、プログラマは完全に新しいデータメンバーとメンバー関数を記述する代わりに、新しいクラスが既存のクラスのメンバーを継承するように指定できます。 この既存のクラスは base クラスと呼ばれ、新しいクラスは derived クラスと呼ばれます。
継承のアイデアは、 IS-A 関係を実装します。 たとえば、哺乳類 IS A 動物、犬 IS-A 哺乳類、したがって犬 IS-A 動物などです。
基本クラスと派生クラス
クラスは複数のクラスまたはインターフェイスから派生できます。つまり、複数の基本クラスまたはインターフェイスからデータと機能を継承できます。
派生クラスを作成するためにC#で使用される構文は次のとおりです-
<acess-specifier> class <base_class> {
...
}
class <derived_class> : <base_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;
}
//Derived class
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);
//Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Total area: 35
基本クラスの初期化
派生クラスは、基本クラスのメンバー変数とメンバーメソッドを継承します。 したがって、サブクラスを作成する前に、スーパークラスオブジェクトを作成する必要があります。 メンバーの初期化リストで、スーパークラスの初期化の指示を与えることができます。
次のプログラムはこれを示しています-
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
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("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {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("Cost: {0}", GetCost());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
C#での多重継承
- 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;
}
//Base class PaintCost
public interface PaintCost {
int getCost(int area);
}
//Derived class
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();
//Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Total area: 35
Total paint cost: $2450