第5章 C# 中的繼承_第1頁
第5章 C# 中的繼承_第2頁
第5章 C# 中的繼承_第3頁
第5章 C# 中的繼承_第4頁
第5章 C# 中的繼承_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論