用于分层继承的 C# 程序
继承是面向对象编程的一个基本方面。超类,也称为基类,是其成员被继承的类,而子类,也称为派生类,是从超类继承的类。它们也分别称为父类和子类。就像孩子继承父母的特征,父母继承他们前辈的特征一样,编程语言中的继承也是如此。
层次继承
就技术术语和面向对象方面而言,它是一种将特征从父类传输到基类、子类或子类的方式。父类或超类是从中获取属性的类,即继承特征。层次继承描述了一个父类被多个子类继承的情况。从单个父类或基类继承多个类的继承类型称为分层继承。基类与父类共享许多相同的属性,尤其是那些在父类中通用的属性。一个基类会产生许多类。这就像有几个孩子,每个孩子都有自己从父母那里获得的一系列特征。例如,在下图中,类 A 作为子类 B、C 和 D 的基类(父类)。
示例 1:
下面例子中的基类是Father,派生类是ChildFirst 和ChildSecond。我们已经从两个派生类创建了对象,并且正在调用相同的基类函数。
C#
// C# program to illustrate the above concept
using System;
// Base Class
public class Father
{
public string FatherName()
{
return "Ravi";
}
}
// Derived Class
public class ChildFirst : Father
{
public string ChildDName()
{
return "Rohan";
}
}
// Derived Class
public class ChildSecond : Father
{
public string ChildDName()
{
return "Nikhil";
}
}
class GFG{
static public void Main()
{
ChildFirst first = new ChildFirst();
// Displaying Child Name and Father Name for
// ChildFirst
Console.WriteLine("My name is " + first.ChildDName() +
". My father name is " +
first.FatherName() + ".");
ChildSecond second = new ChildSecond();
// Displaying Child Name and Father Name for
// ChildSecond
Console.WriteLine("My name is " + second.ChildDName() +
". My father name is " +
second.FatherName() + ".");
}
}
C#
// C# program to illustrate the above concept
using System;
// Base Class
class Person
{
public string name;
public int aadhar_id;
public int age;
public Person(int aadhar_id, int age, string name)
{
this.aadhar_id = aadhar_id;
this.name = name;
this.age = age;
}
}
// Derived Class
class Teacher : Person
{
public int teacher_salary;
public Teacher(int aadhar_id, int salary,
string name, int age) : base(aadhar_id,
age, name)
{
teacher_salary = salary;
}
public void TeacherDetails()
{
Console.WriteLine("teacher ID: " + aadhar_id);
Console.WriteLine("teacher Name: " + name);
Console.WriteLine("teacher Salary: " + teacher_salary);
Console.WriteLine("teacher Age: " + age);
}
}
// Derived Class
class Doctor : Person
{
public int doctor_fees;
public Doctor(int aadhar_id, int fees,
string name, int age) : base(aadhar_id,
age, name)
{
doctor_fees = fees;
}
public void DoctorDetails()
{
Console.WriteLine("Doctor ID: " + aadhar_id);
Console.WriteLine("Doctor Name: " + name);
Console.WriteLine("Doctor Fees: " + doctor_fees);
Console.WriteLine("Doctor Age: " + age);
}
}
class GFG{
static public void Main()
{
// Creating objects
Teacher t = new Teacher(25054, 50000, "Sanjay", 28);
Doctor d = new Doctor(25045, 750, "Rohit", 32);
t.TeacherDetails();
Console.WriteLine(
"-----------------------------------");
d.DoctorDetails();
}
}
输出
My name is Rohan. My father name is Ravi.
My name is Nikhil. My father name is Ravi.
示例 2:
在以下代码中,我们创建了三个类:Person、Teacher 和 Doctor。在这个例子中,Person 类同时被 Teacher 和 Doctor 类继承。每个类中的构造函数用于初始化数据成员。然后我们创建了Teacher 和Doctor 对象并使用TeacherDetails() 和DoctorDetails() 分别为Teacher 和Doctor 生成信息。
C#
// C# program to illustrate the above concept
using System;
// Base Class
class Person
{
public string name;
public int aadhar_id;
public int age;
public Person(int aadhar_id, int age, string name)
{
this.aadhar_id = aadhar_id;
this.name = name;
this.age = age;
}
}
// Derived Class
class Teacher : Person
{
public int teacher_salary;
public Teacher(int aadhar_id, int salary,
string name, int age) : base(aadhar_id,
age, name)
{
teacher_salary = salary;
}
public void TeacherDetails()
{
Console.WriteLine("teacher ID: " + aadhar_id);
Console.WriteLine("teacher Name: " + name);
Console.WriteLine("teacher Salary: " + teacher_salary);
Console.WriteLine("teacher Age: " + age);
}
}
// Derived Class
class Doctor : Person
{
public int doctor_fees;
public Doctor(int aadhar_id, int fees,
string name, int age) : base(aadhar_id,
age, name)
{
doctor_fees = fees;
}
public void DoctorDetails()
{
Console.WriteLine("Doctor ID: " + aadhar_id);
Console.WriteLine("Doctor Name: " + name);
Console.WriteLine("Doctor Fees: " + doctor_fees);
Console.WriteLine("Doctor Age: " + age);
}
}
class GFG{
static public void Main()
{
// Creating objects
Teacher t = new Teacher(25054, 50000, "Sanjay", 28);
Doctor d = new Doctor(25045, 750, "Rohit", 32);
t.TeacherDetails();
Console.WriteLine(
"-----------------------------------");
d.DoctorDetails();
}
}
输出
teacher ID: 25054
teacher Name: Sanjay
teacher Salary: 50000
teacher Age: 28
-----------------------------------
Doctor ID: 25045
Doctor Name: Rohit
Doctor Fees: 750
Doctor Age: 32