English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define another class based on a class, which makes it easier to create and maintain applications. It also facilitates code reuse and saves development time.
When creating a class, the programmer does not need to rewrite the new data members and member functions completely, just design a new class that inherits the members of the existing class. This existing class is calledBase class, this new class is calledDerived class.
The concept of inheritance has realized Belongs to (IS-A) Relationship. For example, Mammal Belongs to (IS-A) Animal, dog Belongs to (IS-A) Mammal, therefore dog Belongs to (IS-A) Animal.
A class can derive from multiple classes or interfaces, which means it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating a derived class in C# is as follows:
<AccessModifier> class <BaseClass>
{
...
}
class <DerivedClass> : <BaseClass>
{
...
}
Suppose, there is a base class Shape, whose derived class is 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);
// Stampa dell'area dell'oggetto
Console.WriteLine("Superficie totale: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
Quando il codice sopra viene compilato ed eseguito, produrrà i seguenti risultati:
Superficie totale: 35
Derived classes inherit the member variables and member methods of the base class. Therefore, the base class object should be created before the derived class object. You can initialize the base class in the member initialization list.
The following program demonstrates this:
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("Costo: {0}", GetCost());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4,5, 7,5);
t.Display();
Console.ReadLine();
}
}
}
Quando il codice sopra viene compilato ed eseguito, produrrà i seguenti risultati:
Lunghezza: 4,5 Larghezza: 7,5 Area: 33,75 Costo: 2362,5
La multieredità si riferisce a una classe che può ereditare funzionalità e caratteristiche da più di un padre contemporaneamente. A differenza della single inheritance, la single inheritance si riferisce a una classe che può ereditare da un solo padre.
C# non supporta la multiereditàMa è possibile implementare la multieredità utilizzando l'interfaccia. Il seguente programma dimostra questo:
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();
// Stampa dell'area dell'oggetto
Console.WriteLine("Superficie totale: {0}", Rect.getArea());
Console.WriteLine("Costo totale della vernice: ${0}", Rect.getCost(area));
Console.ReadKey();
}
}
}
Quando il codice sopra viene compilato ed eseguito, produrrà i seguenti risultati:
Superficie totale: 35 Costo totale della vernice: $2450