文档详情

第5章_特殊函数和成员.ppt

发布:2017-07-16约1.04万字共18页下载文档
文本预览下载声明
* 回顾 class Teacher { public string TeaNO; public string TeaName; public int Age; public string Title; public void SetInformation(string teano,string teaname,int age,string title) { this.TeaNO = teano; this.TeaName = teaname; this.Age = age; this.Title = title; } public void GetInformation() { Console.WriteLine(教师工号为:{0},TeaNO); Console.WriteLine(教师姓名为:{0},TeaName); Console.WriteLine(教师年龄为:{0},Age); Console.WriteLine(教师职称为:{0},Title); } } static void Main(string[] args) { Teacher Mike = new Teacher(); Mike.SetInformation(“001”,”Mike”,21,”professor”); Mike.GetInformation(); } private private private private 第5章 --类的继承与构造函数 目标 理解继承 在C# 中使用继承 理解构造函数和析构函数 * 继承与派生 在已有类的基础上新增自己的特性而产生新类的过程称为继承与派生。 被继承的类称为基类(父类、超类superclass)。 派生出的新类称为派生类(或子类subclass)。 继承的目的:实现代码重用。 派生的目的:当新的问题出现,原有程序无法解决(或不能完全解决)时,需要对原有程序进行改造。 class A { protected int a; public void showa() {Console.Write(a);} } class B: A { int b; public void showab() { Console.Write(“{0},{1}”,a,b); } } 继承来的! * 图1 两类对象具有共性 图2 类的层次结构 实现代码重用 继承示例 图示 假设姓名 与年龄访 问权限为 private * 问题1:成员变量为private时是否被子类继承 class Teacher { private string TeaNO; private string TeaName; private int Age; private string Title; public void SetInformation(string teano,string teaname,int age,string title) { this.TeaNO = teano; this.TeaName = teaname; this.Age = age; this.Title = title; } public void GetInformation() { Console.WriteLine(教师工号为:{0},TeaNO); Console.WriteLine(教师姓名为:{0},TeaName); Console.WriteLine(教师年龄为:{0},Age); Console.WriteLine(教师职称为:{0},Title); } } * class PartTimeTeacher : Teacher { public string OriginalSchool; public vo
显示全部
相似文档