版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、第五章C# 中的繼承 2回顧類是 C# 中的一種結(jié)構(gòu),用于在程序中模擬現(xiàn)實(shí)生活的對象成員變量表示對象的特征方法表示對象可執(zhí)行的操作如果類中未定義構(gòu)造函數(shù),則由運(yùn)行庫提供默認(rèn)構(gòu)造函數(shù)析構(gòu)函數(shù)不能重載,并且每個類只能有一個析構(gòu)函數(shù)可以根據(jù)不同數(shù)量的參數(shù)或不同數(shù)據(jù)類型參數(shù)對方法進(jìn)行重載,不能根據(jù)返回值進(jìn)行方法重載命名空間用來界定類所屬的范圍,類似于Java中的包3目標(biāo)理解繼承在C# 中使用繼承在C#中使用接口在C#中使用方法的重寫4繼承 2-1Class Base / 成員變量 int basevar; / 成員函數(shù) Base_fun1() / 定義 . .Class Derived : Base
2、/ 成員變量 int derivedvars; / 成員函數(shù) Derived_fun1() / 定義 . .基類 void main() Derived dr_obj = new Derived() ; dr_obj.Base_fun1();無需重新編寫代碼派生類5狗馬繼承 2-2動物基類派生類繼承的層次結(jié)構(gòu)示例Class Animal / 成員變量 int eyes, nose; Animal() eyes = 2; nose = 1; Pet_Animal() / 定義 基類Class Dog : Animal / 成員變量 / 成員函數(shù) private Barking() / 定義 pr
3、ivate Wagging_Tail() 派生類6繼承 C# 中的類 public class Graduate: Student, Employee / 成員變量 / 成員函數(shù) 多重繼承 允許多重接口實(shí)現(xiàn)X7演示public class Student:Person private string _school; private uint _eng; private uint _math; private uint _sci; public void GetMarks() Console.WriteLine(“請輸入學(xué)校名稱);_school = Console.ReadLine();Con
4、sole.WriteLine(請分別輸入英語、數(shù)學(xué)和自然科學(xué)的分?jǐn)?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 _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();調(diào)用的基類成員無法實(shí)現(xiàn) GetIn
6、fo() 和 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(尊敬的 0,您的年齡為 1, _name, _age); public class Student:Person
7、 private string _school; private uint _eng; private uint _math; private uint _sci; private uint _tot; public uint GetMarks() Console.WriteLine(“請輸入學(xué)校名稱);_school = Console.ReadLine();Console.WriteLine(請分別輸入英語、數(shù)學(xué)和自然科學(xué)的分?jǐn)?shù)。 ); _eng = uint.Parse(Console.ReadLine();_math = uint.Parse(Console.ReadLine();_s
8、ci = 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)Console.WriteLine(合格);elseConsole.WriteLine(“不合格); 派生類public static v
9、oid Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DispInfo(); objUnderGraduate.ChkEgbl();9用于從派生類中訪問基類成員 可以使用 base 關(guān)鍵字調(diào)用基類的構(gòu)造函數(shù) 關(guān)鍵字 base10調(diào)用 base 構(gòu)造函數(shù) public class Student:Personprivate uint id; /調(diào)用 Person 構(gòu)造函數(shù) public Student(stri
10、ng name,uint age,uint id):base(name,age) this.id = id;Console.WriteLine(id); :base 關(guān)鍵字將調(diào)用 Person 類構(gòu)造函數(shù)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 class S
11、tudent:Person private uint _id; public Student(string name, uint age, uint id):base(name, age) this._id = id;Console.WriteLine(_id); 還將調(diào)用 Base 構(gòu)造函數(shù)static void Main(string args) /構(gòu)造 Student Student objStudent = new Student(XYZ, 45, 001);12關(guān)鍵字 overrideClass Base / 成員變量 int basevar; / 成員函數(shù) GetInfo() /
12、定義 . .Class Derived : Base / 成員變量 int derivedvars; / 成員函數(shù) override GetInfo() / 定義 . .基類派生類base 方法的新實(shí)現(xiàn)13關(guān)鍵字 virtual Access modifier virtual return type name ( parameters-list ) . / Virtual 方法實(shí)現(xiàn) .public virtual void Func()Console.WriteLine(“這是 virtual 方法,可以被重寫);14/將執(zhí)行派生類的變量/要求 new 訪問修飾符將輸出派生類中的 val相同字
13、段new隱藏繼承成員關(guān)鍵字 new15class 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(); objDervEmploye
14、e.EmpInfo(); Employee objEmployee = objDervEmployee; objEmployee.EmpInfo();16抽象類和抽象方法 2-1 abstract class ClassOne /類實(shí)現(xiàn)訪問修飾符派生類的基類不能實(shí)例化17abstract class Base / 成員變量 int basevar; / 成員函數(shù)abstract void base_fun1(parameters); / 無法實(shí)現(xiàn) .抽象方法class Derived : Base / 成員變量 int derivedvars; / 成員函數(shù)override void Base
15、_fun1(parameters) / 實(shí)際實(shí)現(xiàn) .抽象類派生類提供重寫方法原型必須重寫 抽象類和抽象方法 2-2 18演示using System;namespace Example_5abstract class ABCpublic abstract void AFunc();public void BFunc()Console.WriteLine(“這是一個非抽象方法!);class Derv : ABCpublic override void AFunc() Console.WriteLine(“這是一個抽象方法! ); 抽象類 不能實(shí)例化派生類 重寫方法static void Mai
16、n(string args) Derv objB = new Derv(); objB.AFunc(); objB.BFunc();19abstract class MyAbs public abstract void AbMethod();/派生類class MyClass : MyAbspublic override void AbMethod() Console.WriteLine(“在 MyClass 中實(shí)現(xiàn)的抽象方法);/派生自 MyClass 的子類class SubMyClass:MyClasspublic void General() /未實(shí)現(xiàn) AbMethod 抽象方法 Co
17、nsole.WriteLine(在 SubMyClass 中未實(shí)現(xiàn)的抽象方法 );static void Main(string args) SubMyClass objSubClass = new SubMyClass(); objSubClass.General();20接口 4-1OFFON請按開關(guān)按鈕:ON/OFF兩種方法ONOFF21接口 4-2OFFONISwitchON()OFF()22接口 4-3 class IBase void method1();int method2();int method3(float);/沒有實(shí)現(xiàn) .接口interface只有方法聲明沒有實(shí)現(xiàn)23p
18、ublic interface IPictint DeleteImage();void DisplayImage();隱式聲明為 public無訪問修飾符示例中的 IPict 接口用于演示接口接口 4-424演示public class MyImages : IPict /第一個方法的實(shí)現(xiàn) public int DeleteImage() Console.WriteLine(“DeleteImage 實(shí)現(xiàn)!);return(5); /第二個方法的實(shí)現(xiàn) public void DisplayImage() Console.WriteLine(DisplayImage 實(shí)現(xiàn)!); static v
19、oid Main(string args) MyImages objM = new MyImages(); objM.DisplayImage(); int t = objM.DeleteImage(); Console.WriteLine(t);派生自 IPict 接口25演示:示例 10public interface IPictint DeleteImage();void DisplayImage();public class MyImages : BaseIO, IPict public int DeleteImage() Console.WriteLine(“DeleteImage
20、實(shí)現(xiàn)!); return(5); public void DisplayImage() Console.WriteLine(“DisplayImage 實(shí)現(xiàn)!); public class BaseIO 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.
21、Open();26多重接口實(shí)現(xiàn)C# 不允許多重類繼承但 C# 允許多重接口實(shí)現(xiàn) 這意味著一個類可以實(shí)現(xiàn)多個接口 27演示:示例 11public interface IPictManip void ApplyAlpha();/第二個接口public interface IPictint DeleteImage();void DisplayImage();public class BaseIO public void Open() Console.WriteLine(“BaseIO 的 Open 方法); static void Main(string args) MyImages objM =
22、 new MyImages(); objM.DisplayImage(); objM.DeleteImage(); objM.Open(); objM.ApplyAlpha();28顯式接口實(shí)現(xiàn)在 C# 中,只要不發(fā)生命名沖突,就完全可以允許多重接口實(shí)現(xiàn)public interface IPictManip void ApplyAlpha();public interface IPict void ApplyAlpha();public class MyImages : BaseIO, IPict, IPictManippublic int ApplyAlpha().?使用顯式接口實(shí)現(xiàn)29演示
23、:示例 12public class MyImages : BaseIO, IPict, IPictManip public int DeleteImage() Console.WriteLine(“DeleteImage 實(shí)現(xiàn)!);return(5); public void ApplyAlpha() Console.WriteLine(“ApplyAlpha 實(shí)現(xiàn)!); void IPict.DisplayImage() Console.WriteLine(“DisplayImage 的 IPict 實(shí)現(xiàn)); void IPictManip.DisplayImage() Console.W
24、riteLine(“DisplayImage 的 IPictManip 實(shí)現(xiàn)); 顯式接口實(shí)現(xiàn)static void Main(string args)MyImages objM = new MyImages();IPict Pict = objM; /IPict 引用Pict.DisplayImage();IPictManip PictManip = objM; /IPictManip 引用PictManip.DisplayImage();30演示:示例 13public interface IPict int DeleteImage();public interface IPictManip
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 居家保姆雇傭合同書
- 2025年統(tǒng)編版八年級地理上冊月考試卷
- 2025年滬教新版高二數(shù)學(xué)上冊階段測試試卷
- 2025年粵人版八年級歷史下冊階段測試試卷
- 遵義職業(yè)技術(shù)學(xué)院《西方法律思想史(B)》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025年牛棚養(yǎng)殖廢棄物回收與處理服務(wù)合同4篇
- 二零二五版門窗行業(yè)標(biāo)準(zhǔn)化安裝服務(wù)合同4篇
- 二零二五版苗木種植與森林防火技術(shù)服務(wù)合同3篇
- 2025年度新型木門材料研發(fā)與市場拓展合作合同3篇
- 二零二五版木托盤生產(chǎn)設(shè)備進(jìn)出口合同4篇
- 中藥材產(chǎn)地加工技術(shù)規(guī)程 第1部分:黃草烏
- 危險化學(xué)品經(jīng)營單位安全生產(chǎn)考試題庫
- 基于視覺的工業(yè)缺陷檢測技術(shù)
- 案例分析:美國紐約高樓防火設(shè)計(jì)課件
- 老客戶維護(hù)方案
- 移動商務(wù)內(nèi)容運(yùn)營(吳洪貴)任務(wù)一 用戶定位與選題
- 萬科物業(yè)管理公司全套制度(2016版)
- 2021年高考化學(xué)真題和模擬題分類匯編專題20工業(yè)流程題含解析
- 工作證明模板下載免費(fèi)
- (完整word)長沙胡博士工作室公益發(fā)布新加坡SM2考試物理全真模擬試卷(附答案解析)
- 機(jī)械點(diǎn)檢員職業(yè)技能知識考試題庫與答案(900題)
評論
0/150
提交評論