继承和抽象接口

上传人:第*** 文档编号:49625221 上传时间:2018-07-31 格式:PPT 页数:31 大小:1.06MB
返回 下载 相关 举报
继承和抽象接口_第1页
第1页 / 共31页
继承和抽象接口_第2页
第2页 / 共31页
继承和抽象接口_第3页
第3页 / 共31页
继承和抽象接口_第4页
第4页 / 共31页
继承和抽象接口_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《继承和抽象接口》由会员分享,可在线阅读,更多相关《继承和抽象接口(31页珍藏版)》请在金锄头文库上搜索。

1、第五章 C# 中的继承 回顾 类是 C# 中的一种结构,用于在程序中模拟现实生活的对 象 成员变量表示对象的特征 方法表示对象可执行的操作 如果类中未定义构造函数,则由运行库提供默认构造函数 析构函数不能重载,并且每个类只能有一个析构函数 可以根据不同数量的参数或不同数据类型参数对方法进行 重载,不能根据返回值进行方法重载 命名空间用来界定类所属的范围,类似于Java中的包2目标 理解继承 在C# 中使用继承 在C#中使用接口 在C#中使用方法的重写3继承 2-14Class Base/ 成员变量int basevar;/ 成员函数Base_fun1()/ 定义 .Class Derived

2、: Base/ 成员变量int derivedvars;/ 成员函数Derived_fun1()/ 定义.基类 void main() Derived dr_obj = new Derived() ;dr_obj.Base_fun1(); 无需重新编写代码派生类继承 2-25狗马动物基类派生类继承的层次结构示例Class Animal/ 成员变量int eyes, nose;Animal() eyes = 2;nose = 1; Pet_Animal()/ 定义 基类Class Dog : Animal / 成员变量/ 成员函数private Barking() / 定义private Wag

3、ging_Tail() 派生类继承 C# 中的类 6public class Graduate: Student, Employee/ 成员变量/ 成员函数 多重继承 允许多重接口实现7演示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.WriteLin

4、e(“请分别输入英语、数学和自然科学的分数。“); _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(“请输入您的姓名和年龄“

5、);_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() 和 DispI

6、nfo() 方法8演示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); public class Student:Person private string

7、 _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(Console.ReadLine(); _math = uint.Parse(Console.ReadLine(); _sci = uint.Par

8、se(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) Console.WriteLine(“合格“); else Console.WriteLine(“不合格“); 派生类public static

9、void Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate();objUnderGraduate.GetInfo();objUnderGraduate.DispInfo();objUnderGraduate.ChkEgbl(); 9q用于从派生类中访问基类成员 q可以使用 base 关键字调用基类的构造函数 关键字 base调用 base 构造函数 10public class Student:Person private uint id;/调用 Person 构造函数 public Student(str

10、ing name,uint age,uint id):base(name,age) this.id = id; Console.WriteLine(id); :base 关键字将调用 Person 类构造函数11演示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 clas

11、s 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); 关键字 override12Class Base/ 成员变量int basevar;/ 成员函数GetInfo()/

12、 定义 .Class Derived : Base/ 成员变量int derivedvars;/ 成员函数override GetInfo()/ 定义.基类派生类base 方法的新实现关键字 virtual 13Access modifier virtual return type name( parameters-list ) ./ Virtual 方法实现. public virtual void Func() Console.WriteLine(“这是 virtual 方法,可以被重写“); 关键字 new14/将执行派生类的变量/要求 new 访问修饰符将输出派生类中的 val相同字段

13、new隐藏继承成员15class Employee public virtual void EmpInfo() Console.WriteLine(“此方法显示职员信息“); class DervEmployee: Employee public override void EmpInfo() base.EmpInfo(); Console.WriteLine(“此方法重写 base 方法“); static void Main(string args) DervEmployee objDervEmployee = new DervEmployee();objDervEmployee.EmpI

14、nfo();Employee objEmployee = objDervEmployee; objEmployee.EmpInfo(); 抽象类和抽象方法 2-1 16abstract class ClassOne /类实现访问修饰符派生类的基类不能实例化抽象类和抽象方法 2-2 17abstract class Base/ 成员变量int basevar;/ 成员函数abstract void base_fun1(parameters);/ 无法实现.抽象方法class Derived : Base/ 成员变量int derivedvars;/ 成员函数override void Base_

15、fun1(parameters)/ 实际实现.抽象类派生类提供重写方法原型必须重写 18演示using System; namespace Example_5 abstract class ABC public abstract void AFunc(); public void BFunc() Console.WriteLine(“这是一个非抽象方法!“); class Derv : ABC public override void AFunc() Console.WriteLine(“这是一个抽象方法! “); 抽象类 不能实例化派生类 重写方法static void Main(string args) Derv objB = new Derv();objB.AFunc();objB.BFunc(); 19abstract class MyAbs public abstract void AbMethod(); /派生类 class MyClass : MyAbs public override void AbMethod() Console.WriteLine(“在 MyClass 中实现的抽象方法“); /派生自 MyClass 的子类 class Su

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号