5.1 继承的概念
5.1.1 继承的概念
- 继承是指子类可以沿用父类的某些行为和特征。当 A 类被 B 类继承时,我们就把 A 类称为父类或基类,把 B 类称为子类或派生类
- 通过继承,子类自动拥有了父类可被继承的字段、属性和方法等,只需再定义自己独特的成员即可
示例:
class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public void Sound()
{
Console.WriteLine("{0} 发出了叫声。", Name);
}
}
//冒号表示继承关系
class Bird: Animal
{
private int wing;
public int Wing
{
get { return wing; }
set { wing = value; }
}
}
static void Main(string[] args)
{
Bird bird = new Bird();
bird.Name = " 麻雀 ";//继承于动物类的属性
bird.Sound();//继承于动物类的方法
}
5.1.2 继承的使用场合
- 在软件开发的概要设计阶段,若多个类具有相同的属性和方法,便可抽象出相同的属性和方法作为独立的父类
- 在编码阶段一般则是先定义好父类,再根据实际需要创建不同的子类
某公司需开发一套内部办公管理软件
示例:
class Programmer
{
public string name;
public int age;
public int seniority;
public void Meeting()
{
}
public void Development()
{
}
}
class Salesman
{
public string name;
public int age;
public int performance;
public void Meeting()
{
}
public void Sale()
{
}
}
class Personnel
{
public string name;
public int age;
public void Meeting()
{
}
public void Hiring()
{
}
}
通过对比发现,该公司所有员工都要保存姓名和年龄,而且都需要开会。由此可定义员工类为父类来提高代码的重用性
class Employee
{
public string name;
public int age;
public void Meeting()
{
}
}
class Programmer:Employee
{
public int seniority;
public void Development()
{
}
}
class Salesman: Employee
{
public int performance;
public void Sale()
{
}
}
class Consultant: Employee
{
public void Consult()
{
}
}
5.2 继承的实现
5.2.1 继承的特性
在C#的继承中,包含三个重要的特性
- 继承的传递性
- 继承的单根性
- 成员的访问性
5.2.2 C# 的继承运用
- 在 C# 中,子类会先隐式调用父类的无参构造方法,再调用自身的构造方法
- 如果父类没有无参构造方法则提示错误
示例:
class Bird: Animal
{
public Bird ()
{Console.WriteLine(" 这是鸟类的构造方法。");}
}
static void Main(string[] args)
{Bird bird = new Bird();}
class Animal
{
public Animal()
{Console.WriteLine(" 这是动物类的构造方法。");}
//属性和方法代码省略
}
在子类中,可以使用 this 关键字访问本类成员
语法:
this():访问自身构造方法。
this. 成员名称:访问自身成员。
示例:
class Animal
{
public Animal()
{
Console.WriteLine(" 这是动物类的无参构造方法。");
}
public Animal(string name): this()
{
this.Name = name;
Console.WriteLine(" 这是动物类的有参构造方法。");
}
private string name;
public string Name{
get { return name; }
set { name = value;
}
static void Main(string[] args)
{
Animal animal = new Animal (" 老虎 ");
}
在子类中,可以使用 base 关键字访问父类成员
语法:
base():在子类中访问父类构造方法。
base 成员名称:在子类中访问父类成员。
示例:
class Bird: Animal//鸟类继承自动物类
{
public Bird (string name): base(name)//调用父类构造方法
{
Console.WriteLine(" 这是鸟类的有参构造方法。");
}
}
static void Main(string[] args)
{
Bird bird = new Bird (" 大雁 ");
}
在继承关系中,子类和父类满足“子类 is—a 父类”的关系
class Bird : Animal
{
public Bird()
{Console.WriteLine(" 这是鸟类构造方法。");}
}
static void Main(string[] args)//子类和父类存在is-a的关系
{ Animal animal = new Bird();
}
class Animal
{
public Animal()
{Console.WriteLine(" 这是动物类构造方法。");}
}
在 C# 中,如果定义一个没有父类的类,则会自动继承 object 类
示例:
5.3 访问修饰符
5.3.1 访问修饰符介绍
C# 中共有 5 种访问修饰符:public、private、protected、internal 和 protected internal
- public:公有的,访问不受限制
- private:私有的,只能本类内部访问
- protected:受保护的,只能本类及其子类访问
- internal:内部的,只限于本项目内成员访问
- protected internal:内部保护的,只限于本项目或子类访问
5.3.2 访问修饰符的运用
- 新建控制台应用程序 AppOne,在项目中添加 Animal 类,包含 protected 访问性的 Name 属性,internal访问性的 Age 属性
- 在 Program 类中定义一个 Animal 对象,可以发现 Name 属性不能被访问
- 在项目 AppOne 中定义 Cat 类,继承于 Animal 类,观察其可访问成员
总结 :
- 通过继承,子类自动拥有了父类可被继承的字段、属性和方法等,只需再定义自己独特的成员即可
- 在 C# 中,子类会先隐式调用父类的无参构造方法,再调用自身构造方法。如果父类没有无参构造方法,则提示错误
- C# 共有 5 种访问修饰符:public、private、protected、internal 和 protected internal