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 a new class based on an existing class, making it easier to create and maintain applications. It also helps in code reuse and saves development time.
when creating a class, programmers do not need to rewrite new data members and member functions completely. They just need to design a new class that inherits the members of an existing class. This existing class is calledbase class, 이 새로운 클래스는서브 클래스.
inheritance concept is implemented belongs to (IS-A) relationship. For example, mammal belongs to (IS-A) animal, dog belongs to (IS-A) mammal, so dog belongs to (IS-A) 동물.
한 클래스는 여러 클래스나 인터페이스에서 파생될 수 있으며, 이는 클래스나 인터페이스에서 데이터와 함수를 여러 기본 클래스나 인터페이스에서 상속할 수 있음을 의미합니다.
C#에서 파생 클래스를 생성하는 문법은 다음과 같습니다:
<접근 제어자> class <기본 클래스>
{
...
}
class <스플래시 클래스> : <기본 클래스>
{
...
}
예를 들어, 기본 클래스 Shape가 있고, 그 파생 클래스는 Rectangle입니다:
using System;
namespace 상속할당프로그램
{
class Shape
{
public void setWidth(int w)
{
너비 = w;
}
public void setHeight(int h)
{
높이 = h;
}
protected int 너비;
protected int 높이;
}
// 서브 클래스
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleTester
{
static void Main(string[93; args)
{
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// 객체의 면적을 인쇄합니다
Console.WriteLine("총 면적: {0}", Rect.getArea()41;;
Console.ReadKey();
}
}
}
위의 코드가 컴파일되고 실행될 때, 다음과 같은 결과가 생성됩니다:
총 면적: 35
기본 클래스의 멤버 변수와 멤버 메서드를 상속받는 파생 클래스. 따라서 부모 클래스 객체는 자식 클래스 객체가 생성되기 전에 생성되어야 합니다. 멤버 초기화 목록에서 부모 클래스를 초기화할 수 있습니다.
다음 프로그램은 이 점을 보여줍니다:
using System;
namespace RectangleApplication
{
class Rectangle
{
// 멤버 변수
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
너비 = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("길이: {0}", length);
Console.WriteLine("너비: {0}", width);
Console.WriteLine("면적: {0}", GetArea()41;;
}
}//end class Rectangle
class Tabletop : Rectangle
{
private double 비용;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double 비용;
비용 = GetArea() * 70;
return 비용;
}
public void Display()
{
base.Display();
Console.WriteLine("비용: {0}", GetCost()41;;
}
}
class 실행형제테스트
{
static void Main(string[93; 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 상속할당프로그램
{
class Shape
{
public void setWidth(int w)
{
너비 = w;
}
public void setHeight(int h)
{
높이 = h;
}
protected int 너비;
protected int 높이;
}
// 기본 클래스 PaintCost
public interface PaintCost
{
int getCost(int 면적);
}
// 서브 클래스
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int 면적)
{
return 면적 * 70;
}
}
class RectangleTester
{
static void Main(string[93; args)
{
Rectangle Rect = new Rectangle();
int 면적;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// 객체의 면적을 인쇄합니다
Console.WriteLine("총 면적: {0}", Rect.getArea()41;;
Console.WriteLine("油漆 총 비용: ${0}" , Rect.getCost(area)41;;
Console.ReadKey();
}
}
}
위의 코드가 컴파일되고 실행될 때, 다음과 같은 결과가 생성됩니다:
총 면적: 35 油漆 총 비용: $2450