c6屬性委托事件_第1頁
c6屬性委托事件_第2頁
c6屬性委托事件_第3頁
c6屬性委托事件_第4頁
c6屬性委托事件_第5頁
已閱讀5頁,還剩23頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第六章屬性、索引器、委托和事件回顧繼承是獲得現有類的功能的過程創建新類所根據的基礎類稱為基類或父類,新建的類則稱為派生類或子類base

關鍵字用于從派生類中訪問基類成員override關鍵字用于修改方法、屬性或索引器。new訪問修飾符用于顯式隱藏繼承自基類的成員抽象類是指至少包含一個抽象成員(尚未實現的方法)的類。抽象類不能實例化重寫方法就是修改基類中方法的實現。virtual關鍵字用于修改方法的聲明顯式接口實現是用于在名稱不明確的情況下確定成員函數實現的是哪一個接口2目標理解屬性及其不同的類型、實現理解和使用索引器實現委托定義和觸發事件3屬性簡介3-1classEmployee{

privatestaticstring_name;

privatestaticstring_id;

staticvoidMain(string[]args) {

_name=Console.ReadLine();

_id=Console.ReadLine(); }}直接訪問字段

不經驗證

4屬性簡介3-2classEmployee{privatestaticstring_name;privatestaticstring_id;publicvoidSetId(value){ //驗證輸入長度小于2 if(_id.Length>2) _id=value;}publicstringGetId(){ return_id;}}方法SetId(Value)和GetId()分別讀取和寫入職員ID…Employeeemp;emp.SetId("A1");stringDepartment=emp.GetId()…每次都調用GetId()和SetId()方法會很繁瑣屬性5屬性簡介3-3classEmployee{privatestaticstring_name;privatestaticstring_id;publicstringId{ get { return_id; } set { //驗證輸入長度小于2 if(_id.Length>2) _id=value; } }}讀取ID時調用將值賦給ID時調用

6屬性類型

4-3[訪問修飾符]數據類型屬性名{set{};}只寫屬性只能賦值9屬性類型4-4[訪問修飾符]static數據類型屬性名{ get{}; set{};}靜態屬性應用于整個類而不是類的實例只能訪問類的靜態成員10classSavingsAccount{

//類字段用于存儲帳號、余額和已獲利息

privateint_accountNumber;

privatedouble_balance;

privatedouble_interestEarned;

//利率是靜態的,因為所有帳戶獲得的利息相同

privatestaticdouble_interestRate;

//構造函數初始化類成員

publicSavingsAccount(intaccountNumber,doublebalance) {

this._accountNumber=accountNumber;

this._balance

=balance; }

//只讀AccountNumber

屬性

publicintAccountNumber {

get {

return_accountNumber; } }演示……定義和調用屬性4-1

只讀屬性

11定義和調用屬性4-2staticvoidMain(string[]args){

//創建SavingsAccount的對象

SavingsAccountobjSavingsAccount= newSavingsAccount(12345,5000);;

Console.WriteLine("輸入到現在為止已獲得的利息和利率");

objSavingsAccount.InterestEarned= Int64.Parse(Console.ReadLine());

SavingsAccount.InterestRate= Int64.Parse(Console.ReadLine());

objSavingsAccount.InterestEarned+= objSavingsAccount.Balance* SavingsAccount.InterestRate; Console.WriteLine("獲得的總利息為:{0}", objSavingsAccount.InterestEarned);}publicdoubleInterestEarned{

get {

return_interestEarned; }

set {

//驗證數據

if(value<0.0) {

Console.WriteLine(“利息 不能為負數");

return; }

_interestEarned=value; }}將設置InterestEarned屬性12定義和調用屬性4-3staticvoidMain(string[]args){

//創建SavingsAccount的對象

SavingsAccountobjSavingsAccount= newSavingsAccount(12345,5000);;

Console.WriteLine("輸入到現在為止已獲得的利息和利率");

objSavingsAccount.InterestEarned= Int64.Parse(Console.ReadLine());

SavingsAccount.InterestRate= Int64.Parse(Console.ReadLine());

objSavingsAccount.InterestEarned+= objSavingsAccount.Balance* SavingsAccount.InterestRate; Console.WriteLine("獲得的總利息為:{0}", objSavingsAccount.InterestEarned);}將設置InterestRate屬性publicstaticdoubleInterestRate{

get {

return_interestRate; }

set {

//驗證數據

if(value<0.0) {

Console.WriteLine(“利率不能為負數");

return; }

else {

_interestRate=value/100; }}13定義和調用屬性4-4staticvoidMain(string[]args){

//創建SavingsAccount的對象

SavingsAccountobjSavingsAccount= newSavingsAccount(12345,5000);;

Console.WriteLine(“輸入到現在為止已獲得的利息和利率");

objSavingsAccount.InterestEarned= Int64.Parse(Console.ReadLine());

SavingsAccount.InterestRate= Int64.Parse(Console.ReadLine());

objSavingsAccount.InterestEarned+= objSavingsAccount.Balance* SavingsAccount.InterestRate; Console.WriteLine("獲得的總利息為:{0}", objSavingsAccount.InterestEarned);}將檢索Balance和InterestRate屬性publicdoubleBalance{

get {

if(_balance<0)

Console.WriteLine("沒有可用余額");

return_balance; }}14索引器[訪問修飾符]數據類型this[數據類型標識符]{ get{}; set{};}語法15定義和調用索引器4-1演示……classPhoto{

string_title;

publicPhoto(stringtitle) {

this._title=title; }

publicstringTitle {

get {

return_title; } }}以Title屬性表示照片將照片存放于數組photos中classAlbum {

//該數組用于存放照片

Photo[]photos;

publicAlbum(intcapacity) {

photos=newPhoto[capacity]; }16定義和調用索引器

4-2publicPhotothis[intindex]{

get {

//驗證索引范圍

if(index<0||index>=photos.Length) {

Console.WriteLine("索引無效");

//使用null指示失敗

returnnull; }

//對于有效索引,返回請求的照片

returnphotos[index]; }

set {

if(index<0||index>=photos.Length) {

Console.WriteLine("索引無效");

return; }

photos[index]=value; }}帶有int參數的Photo索引器讀/寫索引器17定義和調用索引器4-3publicPhotothis[stringtitle]{

get {

//遍歷數組中的所有照片

foreach(Photopinphotos) {

//將照片中的標題與索引器參數進行比較

if(p.Title==title)

returnp; }

Console.WriteLine("未找到");

//使用null指示失敗

returnnull; }}帶有string參數的Photo索引器只讀索引器18定義和調用索引器4-4staticvoidMain(string[]args){

//創建一個容量為3的相冊

Albumfriends=newAlbum(3);

//創建3張照片

Photofirst=newPhoto("Jenn");

Photosecond=newPhoto("Smith");

Photothird=newPhoto("Mark");

//向相冊加載照片

friends[0]=first;

friends[1]=second;

friends[2]=third;

//按索引檢索

Photoobj1Photo=friends[2];

Console.WriteLine(obj1Photo.Title);

//按名稱檢索

Photoobj2Photo=friends["Jenn"];

Console.WriteLine(obj2Photo.Title);}

19委托Multiply(int,int){….}Divide(int,int){….}在運行時確定調用哪種方法委托和方法必須具有相同的簽名---publicdelegateCall(intnum1,intnum2);---20定義委托2-1[訪問修飾符]delegate返回類型委托名();

語法classDelegates{publicdelegateintCall(intnum1,intnum2);classMath{publicintMultiply(intnum1,intnum2){//實現}publicintDivide(intnum1,intnum2){//實現}}classTestDelegates{staticvoidMain(){ CallobjCall;MathobjMath=newMath(); objCall=newCall(objMath.Multiply);}}將方法與委托關聯起來21定義委托2-2classDelegates{ //委托定義

publicdelegateintCall(intnum1,intnum2);

classMath {

//乘法方法

publicintMultiply(intnum1,intnum2) {

returnnum1*num2; }

//除法方法

publicintDivide(intnum1,intnum2) {

returnnum1/num2; } }staticvoidMain(string[]args){

//委托的對象

CallobjCall;

//Math類的對象

MathobjMath=newMath();

//將方法與委托關聯起來

objCall=newCall(objMath.Multiply);

//將委托實例化

result=objCall(5,3);

System.Console.WriteLine("結果為{0}", result);

}

將方法與委托關聯起來22事件搶答者宣布人搶答者“請聽題~”集中注意力聆聽其他人事件源事件的發布者事件的訂閱人不關心未訂閱該事件定義事件

為對象訂閱該事件將發生的事件通知給訂閱人23定義事件[訪問修飾符]event委托名事件名;

語法publicdelegatevoiddelegateMe();privateeventdelegateMeeventMe;24訂閱事件eventMe+=newdelegateMe(objA.Method);eventMe+=newdelegateMe(objB.Method);25通知訂閱對象if(condition){

eventMe();}調用訂閱特定事件的對象的所有委托26演示:示例4示例classTestEvents{

[STAThread]

staticvoidMain(string[]args) {

//委托的對象

DelegateobjDelegate=newDelegate();

//ClassA的對象

ClassAobjClassA=newClassA();

//ClassB的對象

ClassBobjClassB=newClassB();

//訂閱該事件

objDelegate.NotifyEveryOne+= newDelegate.MeDelegate(objClassA.DispMethod);

objDelegate.NotifyEveryOne+= newDelegate.MeDelegate(obj

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論