
ZJWCHC 第八章 2☑/7”8 C#高级编程
ZJWCHC 第八章 C#高级编程

目标 ◆理解继承 ◆在C#中使用继承 ◆在C#中使用接口 ◆在C#中使用方法的重写 ◆理解属性及其不同的类型、实现 ◆理解和使用索引器 ◆实现委托 ◆定义和触发事件
2 目标 理解继承 在C# 中使用继承 在C#中使用接口 在C#中使用方法的重写 理解属性及其不同的类型、实现 理解和使用索引器 实现委托 定义和触发事件

继承2-1 基类 派生类 Class Base ∥成员变量 int Class Derived:Base ∥月 ∥成员变量 Bas int derivedvars: ∥成员函数 Derived 无需重新编写代码 ∥ void main( Derived dr_obj=new Derived(); dr_obj.Base_fun10月
3 继承 2-1 Class Base { // 成员变量 int basevar; // 成员函数 Base_fun1() { // 定义 } ……. ……. Class Derived : Base { // 成员变量 int derivedvars; // 成员函数 Derived_fun1() { // 定义 } ……. ……. 基类 void main() { Derived dr_obj = new Derived() ; dr_obj.Base_fun1(); } 无需重新编写代码 派生类

继承2-2 Class Animal 基类 结构示例 ∥成员变量 其举 int eyes,nose 派生类 Animal() Class Dog Animal ∥成员变量 eyes =2; ∥成员函数 nose =1; private Barking() ∥定义 Pet Animal() private Wagging_Tail() ∥定义 4
4 狗 马 继承 2-2 动物 基类 派生类 继承的层次结构示例 Class Animal { // 成员变量 int eyes, nose; Animal() { eyes = 2; nose = 1; } Pet_Animal() { // 定义 } } 基类 Class Dog : Animal { // 成员变量 // 成员函数 private Barking() { // 定义 } private Wagging_Tail() { } } 派生类

继承C#中的类 多重继承 public class Graduate:Stude loyee 允许多重接口实现 ∥成员函数
5 继承 C# 中的类 public class Graduate: Student, Employee { // 成员变量 // 成员函数 } 多重继承 允许多重接口实现

public class Person 基类 private string name; mmhh比业口 private uint age; 无法实现GetInfo(0和 public void GetInfo() Displnfo0方法 Console..WriteLine(请输入您的姓名和年龄"); name Console.ReadLine(); age uint.Parse(Console.ReadLine()); public void DispInfo() static void Main(string[]args) Console.WriteLine(' Student obiStudent new Student(; Console.Writ objStudent.Getlnfo(); obiStudent.Displnfo(: objStudent.GetMarks();
6 演示 public class Student:Person { private string _school; private uint _eng; private uint _math; private uint _sci; public void GetMarks() { Console.WriteLine(“请输入学校名称"); _school = Console.ReadLine(); Console.WriteLine("请分别输入英语、数学和自然科学的分数。"); _eng = uint.Parse(Console.ReadLine()); _math = uint.Parse(Console.ReadLine()); _sci = uint.Parse(Console.ReadLine()); Console.WriteLine(“所得总分为:{0}",_eng+_math+_sci); } } 派生类 public class Person { private string _name; private uint _age; public void GetInfo() { Console.WriteLine("请输入您的姓名和年龄"); _name = Console.ReadLine(); _age = uint.Parse(Console.ReadLine()); } public void DispInfo() { Console.WriteLine("尊敬的 {0},您的年龄为 {1} ", _name, _age); } } 基类 static void Main(string[] args) { Student objStudent = new Student(); objStudent.GetInfo(); objStudent.DispInfo(); objStudent.GetMarks(); } 调用的基类成员 无法实现 GetInfo() 和 DispInfo() 方法

put 米 H派生类 public class UnderGraduate:Student pr 派生类 public void Check() pr pr Console,WriteLine(要上升一级,要求总分不低于 pr 150"): pr if(this.GetMarks()>=150) { Console.WriteLine("pass"); else Console.WriteLine("not pass"); public static void Main(string[]args) UnderGraduate objUnderGraduate=new UnderGraduate(); objUnderGraduate.Getlnfo(); objUnderGraduate.DisplayInfo(; objUnderGraduate.Check(); 刀N
7 演示 public class Person { private string _name; private uint _age; public void GetInfo() { Console.WriteLine("请输入您的姓名和年龄"); _name = Console.ReadLine(); _age = uint.Parse(Console.ReadLine()); } public void DislayInfo() { Console.WriteLine("尊敬的 {0},您的年龄为 {1}", _name, _age); } } public class Student:Person { private string _schoolname; private uint _engscore; private uint _mathscore; private uint _sciscore; private uint _totscore; public uint GetMarks() { Console.WriteLine(“请输入学校名称"); _schoolname = Console.ReadLine(); Console.WriteLine("请输入英语、数学和自然科学的分数。 "); _engscore = uint.Parse(Console.ReadLine()); _mathscore = uint.Parse(Console.ReadLine()); _sciscore = uint.Parse(Console.ReadLine()); _totscore = _engscore + _mathscore + _sciscore; Console.WriteLine("所得总分为:{0} ",_totscore); return _totscore; }} 基类 派生类 public class UnderGraduate:Student { public void Check() { Console.WriteLine("要上升一级,要求总分不低于 150 "); if(this.GetMarks() >=150) Console.WriteLine(“pass"); else Console.WriteLine(“not pass"); } } 派生类 public static void Main(string[] args) { UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DisplayInfo(); objUnderGraduate.Check(); }

关键字base 口用于从派生类中访问基类成员 口可以使用base关键字调用基类的构造函数
8 ❑用于从派生类中访问基类成员 ❑可以使用 base 关键字调用基类的构造函数 关键字 base

调用base构造函数 public class Student:Person { private uint id: ∥调用Person构造函数 public Student(string name,uint age,uint id):base(name,age) :base关键字将调用Person类构造函数 9
9 调用 base 构造函数 public class Student:Person { private uint id; //调用 Person 构造函数 publicStudent(string name,uint age,uint id):base(name,age) { this.id = id; Console.WriteLine(id); } } :base 关键字将调用 Person 类构造函数

public class Person public string name; public uint age; public Person(string name,uint age) static void Main(string[]args) /构造Student Student objStudent new Student("XYZ",45,001); p心o心mmoTomogoyomromTromoyoy心 this.idid; Console.WriteLine(_id); 演示 10
10 演示 public class Person { public string _name; public uint _age; public Person(string name, uint age) { this._name = name; this._age = age; Console.WriteLine(_name); Console.WriteLine(_age); } } public class Student:Person { private uint _id; public Student(string name, uint age, uint id):base(name, age) { this._id = id; Console.WriteLine(_id); } } 还将调用 Base 构造函数 static void Main(string[] args) { //构造 Student Student objStudent = new Student("XYZ", 45, 001); }