版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、第五章,C# 中的繼承,回顧,類是 C# 中的一種結(jié)構,用于在程序中模擬現(xiàn)實生活的對象 成員變量表示對象的特征 方法表示對象可執(zhí)行的操作 如果類中未定義構造函數(shù),則由運行庫提供默認構造函數(shù) 析構函數(shù)不能重載,并且每個類只能有一個析構函數(shù) 可以根據(jù)不同數(shù)量的參數(shù)或不同數(shù)據(jù)類型參數(shù)對方法進行重載,不能根據(jù)返回值進行方法重載 命名空間用來界定類所屬的范圍,類似于Java中的包,2,目標,理解繼承 在C# 中使用繼承 在C#中使用接口 在C#中使用方法的重寫,3,繼承 2-1,4,Class Base / 成員變量 int basevar; / 成員函數(shù) Base_fun1() / 定義 . .,Cl
2、ass Derived : Base / 成員變量 int derivedvars; / 成員函數(shù) Derived_fun1() / 定義 . .,基類,void main() Derived dr_obj = new Derived() ; dr_obj.Base_fun1(); ,無需重新編寫代碼,派生類,繼承 2-2,5,狗,馬,動物,基類,派生類,繼承的層次結(jié)構示例,Class Animal / 成員變量 int eyes, nose; Animal() eyes = 2; nose = 1; Pet_Animal() / 定義 ,基類,Class Dog : Animal / 成員變
3、量 / 成員函數(shù) private Barking() / 定義 private Wagging_Tail() ,派生類,繼承 C# 中的類,6,public class Graduate: Student, Employee / 成員變量 / 成員函數(shù) ,多重繼承,允許多重接口實現(xiàn),7,演示,public class Student:Person private string _school; private uint _eng; private uint _math; private uint _sci; public void GetMarks() Console.WriteLine(“請
4、輸入學校名稱); _school = Console.ReadLine(); Console.WriteLine(請分別輸入英語、數(shù)學和自然科學的分數(shù)。); _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 _
5、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.Di
6、spInfo(); objStudent.GetMarks(); ,調(diào)用的基類成員,無法實現(xiàn) GetInfo() 和 DispInfo() 方法,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(尊
7、敬的 0,您的年齡為 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(請分別輸入英語、數(shù)學和自然科學的分數(shù)。 ); _eng = uint.Parse(C
8、onsole.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) C
9、onsole.WriteLine(合格); else Console.WriteLine(“不合格); ,派生類,public static void Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DispInfo(); objUnderGraduate.ChkEgbl(); ,9,用于從派生類中訪問基類成員 可以使用 base 關鍵字調(diào)用基類的構造函數(shù),關鍵字 base,調(diào)用 base 構造函數(shù),10,pu
10、blic class Student:Person private uint id; /調(diào)用 Person 構造函數(shù) public Student(string name,uint age,uint id):base(name,age) this.id = id; Console.WriteLine(id); ,:base 關鍵字將調(diào)用 Person 類構造函數(shù),11,演示,public class Person public string _name; public uint _age; public Person(string name, uint age) this._name = na
11、me; 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); ,還將調(diào)用 Base 構造函數(shù),static void Main(string args) /構造 Student Student objStudent =
12、new Student(XYZ, 45, 001); ,關鍵字 override,12,Class Base / 成員變量 int basevar; / 成員函數(shù) GetInfo() / 定義 . .,Class Derived : Base / 成員變量 int derivedvars; / 成員函數(shù) override GetInfo() / 定義 . .,基類,派生類,base 方法的新實現(xiàn),關鍵字 virtual,13,Access modifier virtual return type name ( parameters-list ) . / Virtual 方法實現(xiàn) . ,publ
13、ic virtual void Func() Console.WriteLine(“這是 virtual 方法,可以被重寫); ,關鍵字 new,14,/將執(zhí)行派生類的變量,/要求 new 訪問修飾符,將輸出派生類中的 val,相同字段,new,隱藏繼承成員,15,class Employee public virtual void EmpInfo() Console.WriteLine(“此方法顯示職員信息); ,class DervEmployee: Employee public override void EmpInfo() base.EmpInfo(); Console.WriteL
14、ine(“此方法重寫 base 方法); ,static void Main(string args) DervEmployee objDervEmployee = new DervEmployee(); objDervEmployee.EmpInfo(); Employee objEmployee = objDervEmployee; objEmployee.EmpInfo(); ,抽象類和抽象方法 2-1,16,abstract class ClassOne /類實現(xiàn) ,訪問修飾符,派生類的基類,不能實例化,抽象類和抽象方法 2-2,17,abstract class Base / 成員變
15、量 int basevar; / 成員函數(shù) abstract void base_fun1(parameters); / 無法實現(xiàn) . ,抽象方法,class Derived : Base / 成員變量 int derivedvars; / 成員函數(shù) override void Base_fun1(parameters) / 實際實現(xiàn) . ,抽象類,派生類,提供,重寫方法,原型,必須重寫,18,演示,using System; namespace Example_5 abstract class ABC public abstract void AFunc(); public void BFu
16、nc() Console.WriteLine(“這是一個非抽象方法!); class Derv : ABC public override void AFunc() Console.WriteLine(“這是一個抽象方法! ); ,抽象類 不能實例化,派生類 重寫方法,static void Main(string args) Derv objB = new Derv(); objB.AFunc(); objB.BFunc(); ,19,abstract class MyAbs public abstract void AbMethod(); /派生類 class MyClass : MyAb
17、s public override void AbMethod() Console.WriteLine(“在 MyClass 中實現(xiàn)的抽象方法); /派生自 MyClass 的子類 class SubMyClass:MyClass public void General() /未實現(xiàn) AbMethod 抽象方法 Console.WriteLine(在 SubMyClass 中未實現(xiàn)的抽象方法 ); ,static void Main(string args) SubMyClass objSubClass = new SubMyClass(); objSubClass.General(); ,接
18、口 4-1,20,OFF,ON,請按開關按鈕:ON/OFF,兩種方法 ON OFF,接口 4-2,21,ISwitch ON() OFF(),接口 4-3,22,class IBase void method1(); int method2(); int method3(float); /沒有實現(xiàn) . ,接口,interface,只有方法聲明,沒有實現(xiàn),23,public interface IPict int DeleteImage(); void DisplayImage(); ,隱式聲明為 public,無訪問修飾符,示例中的 IPict 接口用于演示接口,接口 4-4,24,演示,pu
19、blic class MyImages : IPict /第一個方法的實現(xiàn) public int DeleteImage() Console.WriteLine(“DeleteImage 實現(xiàn)!); return(5); /第二個方法的實現(xiàn) public void DisplayImage() Console.WriteLine(DisplayImage 實現(xiàn)!); ,static void Main(string args) MyImages objM = new MyImages(); objM.DisplayImage(); int t = objM.DeleteImage(); Con
20、sole.WriteLine(t); ,派生自 IPict 接口,25,演示:示例 10,public interface IPict int DeleteImage(); void DisplayImage(); public class MyImages : BaseIO, IPict public int DeleteImage() Console.WriteLine(“DeleteImage 實現(xiàn)!); return(5); public void DisplayImage() Console.WriteLine(“DisplayImage 實現(xiàn)!); ,public class Ba
21、seIO public void Open() Console.WriteLine(BaseIO 的 Open 方法); ,static void Main(string args) MyImages objM = new MyImages(); objM.DisplayImage(); int val = objM.DeleteImage(); Console.WriteLine(val); objM.Open(); ,多重接口實現(xiàn),C# 不允許多重類繼承 但 C# 允許多重接口實現(xiàn) 這意味著一個類可以實現(xiàn)多個接口,26,27,演示:示例 11,public interface IPictM
22、anip void ApplyAlpha(); /第二個接口 public interface IPict int DeleteImage(); void DisplayImage(); public class BaseIO public void Open() Console.WriteLine(“BaseIO 的 Open 方法); ,static void Main(string args) MyImages objM = new MyImages(); objM.DisplayImage(); objM.DeleteImage(); objM.Open(); objM.ApplyAl
23、pha(); ,顯式接口實現(xiàn),28,在 C# 中,只要不發(fā)生命名沖突,就完全可以允許多重接口實現(xiàn),public interface IPictManip void ApplyAlpha(); ,public interface IPict void ApplyAlpha(); ,public class MyImages : BaseIO, IPict, IPictManip public int ApplyAlpha() . . ,?,使用顯式接口實現(xiàn),29,演示:示例 12,public class MyImages : BaseIO, IPict, IPictManip public i
24、nt DeleteImage() Console.WriteLine(“DeleteImage 實現(xiàn)!); return(5); public void ApplyAlpha() Console.WriteLine(“ApplyAlpha 實現(xiàn)!); void IPict.DisplayImage() Console.WriteLine(“DisplayImage 的 IPict 實現(xiàn)); void IPictManip.DisplayImage() Console.WriteLine(“DisplayImage 的 IPictManip 實現(xiàn)); ,顯式接口實現(xiàn),static void Main(string args) MyImages objM = new MyImages(); IPict Pict = objM; /IPict 引用 Pict.DisplayImage(); IPictManip PictManip = objM; /IPictManip 引用 PictManip.DisplayImage(); ,30,演示:示例 13,public interface IPict int DeleteImage(); public interf
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 口腔設備組裝調(diào)試工安全宣貫知識考核試卷含答案
- 制球工安全技能測試水平考核試卷含答案
- 2025四川達州萬源市招聘社區(qū)專職工作者16人備考題庫附答案
- 2025年《職業(yè)能力傾向測驗》常識判斷考核試題(各地真題)
- 涂料生產(chǎn)工操作能力考核試卷含答案
- 珍珠巖加工工測試驗證考核試卷含答案
- 氣體分離工崗前班組安全考核試卷含答案
- 管廊運維員QC管理模擬考核試卷含答案
- 墨錠制作工班組建設競賽考核試卷含答案
- 2024年湖北理工學院輔導員考試筆試真題匯編附答案
- 生物醫(yī)藥研發(fā)項目立項報告
- 2026年中國禮品行業(yè)展望白皮書
- 2025年度校長述職報告:守正中求變用心辦好這所“小而美”的學校
- 2025湖北省考申論縣鄉(xiāng)卷真題及答案
- 國內(nèi)外企業(yè)管理研究現(xiàn)狀的綜述
- 餐廳后廚述職報告
- 數(shù)字化工地培訓
- 2025年七年級上學期期末數(shù)學試卷含答案(共四套)
- 監(jiān)控室值班操作流程標準化
- 2025年上海市事業(yè)單位招聘考試教師招聘體育學科專業(yè)知識試卷(綜合)
- 普貨運輸安全培訓內(nèi)容課件
評論
0/150
提交評論