国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
this 關(guān)鍵字

this 關(guān)鍵字將引用類(lèi)的當(dāng)前實(shí)例。靜態(tài)成員函數(shù)沒(méi)有 this 指針。this關(guān)鍵字可用于從構(gòu)造函數(shù)、實(shí)例方法和實(shí)例訪問(wèn)器中訪問(wèn)成員。

以下是 this 的常用用途:

  • 限定被相似的名稱(chēng)隱藏的成員,例如:
    public Employee(string name, string alias) 
    {
    this.name = name;
    this.alias = alias;
    }
  • 將對(duì)象作為參數(shù)傳遞到其他方法,例如:
    CalcTax(this);
  • 聲明索引器,例如:
    public int this [int param]
    {
    get
    {
    return array[param];
    }
    set
    {
    array[param] = value;
    }
    }

在靜態(tài)方法、靜態(tài)屬性訪問(wèn)器或字段聲明的變量初始值設(shè)定項(xiàng)中引用 this 是錯(cuò)誤的。

示例

在本例中,this 用于限定 Employee 類(lèi)成員 namealias,它們都被相似的名稱(chēng)隱藏。this還用于將對(duì)象傳遞到屬于其他類(lèi)的方法 CalcTax。

// keywords_this.cs
// this example
using System;
public class Employee
{
public string name;
public string alias;
public decimal salary = 3000.00m;

// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}

// Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
}
}
public class Tax
{
public static decimal CalcTax(Employee E)
{
return (0.08m*(E.salary));
}
}

public class MainClass
{
public static void Main()
{
// Create objects:
Employee E1 = new Employee ("John M. Trainer", "jtrainer");

// Display results:
E1.printEmployee();
}
}

輸出

Name: John M. Trainer
Alias: jtrainer
Taxes: $240.00

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C#類(lèi)與對(duì)象
Struts2教程
delegate?useing?for?sort
顯示聲明構(gòu)造方法
重寫(xiě) compareTo后,ArrayList.sort可以升序排列,為什么一般不改成降序呢 ?
構(gòu)造函數(shù)與方法的覆蓋
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服