C# - 継承
オブジェクト指向プログラミングで最も重要な概念の 1 つは、継承です。継承により、別のクラスに関してクラスを定義できるため、アプリケーションの作成と保守が容易になります。これにより、コード機能を再利用して実装時間を短縮する機会も提供されます。
クラスを作成するとき、完全に新しいデータ メンバーとメンバー関数を記述する代わりに、プログラマーは、新しいクラスが既存のクラスのメンバーを継承する必要があることを指定できます。この既存のクラスはベースと呼ばれます クラスであり、新しいクラスは派生と呼ばれます クラス。
継承の考え方は 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
C言語