第四讲C中的继承ppt课件.ppt
《第四讲C中的继承ppt课件.ppt》由会员分享,可在线阅读,更多相关《第四讲C中的继承ppt课件.ppt(35页珍藏版)》请在三一文库上搜索。
1、C# 中的继承,西源软件培训中心,回顾,类是 C# 中的一种结构,用于在程序中模拟现实生活的对象 成员变量表示对象的特征 方法表示对象可执行的操作 如果类中未定义构造函数,则由运行库提供默认构造函数 析构函数不能重载,并且每个类只能有一个析构函数 可以根据不同数量的参数或不同数据类型参数对方法进行重载,不能根据返回值进行方法重载 命名空间用来界定类所属的范围,类似于Java中的包,目标,理解继承 在C# 中使用继承 在C#中使用接口 在C#中使用方法的重写,继承,Class Base / 成员变量 int basevar; / 成员函数 Base_fun1() / 定义 . .,Class D
2、erived : Base / 成员变量 int derivedvars; / 成员函数 Derived_fun1() / 定义 . .,基类,void main() Derived dr_obj = new Derived() ; dr_obj.Base_fun1(); ,无需重新编写代码,派生类,狗,马,继承,动物,基类,派生类,继承的层次结构示例,Class Animal / 成员变量 int eyes, nose; Animal() eyes = 2; nose = 1; Pet_Animal() / 定义 ,基类,Class Dog : Animal / 成员变量 / 成员函数 pr
3、ivate Barking() / 定义 private Wagging_Tail() ,派生类,继承 C# 中的类,public class Graduate: Student, Employee / 成员变量 / 成员函数 ,多重继承,允许多重接口实现,演示,public class Student:Person private string _school; private uint _eng; private uint _math; private uint _sci; public void GetMarks() Console.WriteLine(“请输入学校名称“); _scho
4、ol = 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
5、 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(
6、); objStudent.GetMarks(); ,调用的基类成员,无法实现 GetInfo() 和 DispInfo() 方法,演示,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,您
7、的年龄为 1“, _name, _age); ,public class Student:Person private string _school; private uint _eng; private uint _math; private uint _sci; private uint _tot; public uint GetMarks() Console.WriteLine(“请输入学校名称“); _school = Console.ReadLine(); Console.WriteLine(“请分别输入英语、数学和自然科学的分数。 “); _eng = uint.Parse(Con
8、sole.ReadLine(); _math = uint.Parse(Console.ReadLine(); _sci = uint.Parse(Console.ReadLine(); _tot = _eng + _math + _sci; Console.WriteLine(“所得总分为:0 “,_tot); return _tot; ,基类,派生类,public class UnderGraduate:Student public void ChkEgbl() Console.WriteLine(“要上升一级,要求总分不低于 150 “); if(this.GetMarks() 149)
9、 Console.WriteLine(“合格“); else Console.WriteLine(“不合格“); ,派生类,public static void Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DispInfo(); objUnderGraduate.ChkEgbl(); ,用于从派生类中访问基类成员 可以使用 base 关键字调用基类的构造函数,关键字 base,调用 base 构造函数,pu
10、blic class Student:Person private uint id; /调用 Person 构造函数 public Student(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) this._name = name;
11、 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
12、 Student(“XYZ“, 45, 001); ,关键字 override,Class Base / 成员变量 int basevar; / 成员函数 GetInfo() / 定义 . .,Class Derived : Base / 成员变量 int derivedvars; / 成员函数 override GetInfo() / 定义 . .,基类,派生类,base 方法的新实现,关键字 virtual,Access modifier virtual return type name ( parameters-list ) . / Virtual 方法实现 . ,public virt
13、ual void Func() Console.WriteLine(“这是 virtual 方法,可以被重写“); ,/将执行派生类的变量,/要求 new 访问修饰符,将输出派生类中的 val,相同字段,new,隐藏继承成员,关键字 new,class Employee public virtual void EmpInfo() Console.WriteLine(“此方法显示职员信息“); ,class DervEmployee: Employee public override void EmpInfo() base.EmpInfo(); Console.WriteLine(“此方法重写
14、base 方法“); ,static void Main(string args) DervEmployee objDervEmployee = new DervEmployee(); objDervEmployee.EmpInfo(); Employee objEmployee = objDervEmployee; objEmployee.EmpInfo(); ,抽象类和抽象方法,abstract class ClassOne /类实现 ,访问修饰符,派生类的基类,不能实例化,abstract class Base / 成员变量 int basevar; / 成员函数 abstract vo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第四 中的 继承 ppt 课件
链接地址:https://www.31doc.com/p-2573591.html