C#語法學習this&&base

this 關鍵字所指的是類別 (Class) 的目前執行個體 (Instance),而且也用來當做擴充方法之第一個參數的修飾詞 (Modifier)。
以下為 this 的常見用法:

 

this關鍵字使用的地方:
1,索引
2,this簡單的說,表示所在類.準確的說是代表類的對像.
3,其他場合.代表構造函數

//要限定被類似名稱所隱藏的成員,例如:
public Employee(string name, string alias) 
{
    
this.name = name;
    
this.alias = alias;


//要將物件做為參數傳遞到其他方法,例如:
CalcTax(this);
 

//要宣告索引子,例如:
public int this [int param]
{
    
get { return array[param];  }
    
set { array[param] = value; }
}
//因為靜態成員函式存在於類別層級,且非物件的一部分,所以不具有 this 指標。在靜態方法中參考 this 是錯誤的。



base 關鍵字用於存取衍生類別中的基底類別 (Base Class) 成員:
呼叫一個已被其他方法覆寫之基底類別中的方法。
指定建立衍生類別執行個體時,所要呼叫的基底類別建構函式。
基底類別只允許在建構函式、執行個體方法 (Instance Method) 或執行個體屬性存取子中存取。
在靜態方法中使用 base 關鍵字是錯誤的。

//於此例中,基底類別 Person 和衍生類別 Employee 都有一個名為 Getinfo 的方法。使用 //base 關鍵字,就可以從衍生類別中呼叫基底類別的 Getinfo 方法。
// Accessing base class members
using System;
public class Person
{
    
protected string ssn = "444-55-6666";
    
protected string name = "John L. Malgraine";

    
public virtual void GetInfo()
    {
        Console.WriteLine(
"Name: {0}", name);
        Console.WriteLine(
"SSN: {0}", ssn);
    }
}
class Employee : Person
{
    
public string id = "ABC567EFG";
    
public override void GetInfo()
    {
        
// Calling the base class GetInfo method:
        base.GetInfo();
        Console.WriteLine(
"Employee ID: {0}", id);
    }
}

class TestClass
{
    
static void Main()
    {
        Employee E 
= new Employee();
        E.GetInfo();
    }
}
 

//此範例顯示了在建立衍生類別的執行個體時,要如何指定呼叫的基底類別建構函式。

using System;
public class BaseClass
{
    
int num;

    
public BaseClass()
    {
        Console.WriteLine(
"in BaseClass()");
    }

    
public BaseClass(int i)
    {
        num 
= i;
        Console.WriteLine(
"in BaseClass(int i)");
    }

    
public int GetNum()
    {
        
return num;
    }
}

public class DerivedClass : BaseClass
{
    
// This constructor will call BaseClass.BaseClass()
    public DerivedClass() : base()
    {
    }

    
// This constructor will call BaseClass.BaseClass(int i)
    public DerivedClass(int i) : base(i)
    {
    }

    
static void Main()
    {
        DerivedClass md 
= new DerivedClass();
        DerivedClass md1 
= new DerivedClass(1);
    }
}

/*base
簡單點的說,代表直接父類.
如果子類的父類還有父類,這時base只能訪問到它的直接父類
也就說說父類的父類是沒有辦法訪問到的.
1,使用base可以訪問父類中的成員
2,使用父類構造函數.
 * 
 
*/
using System;
using Traffic;

namespace Test
{
    
class Test
    {
        
static void Main()
        {
            Car theCar1
=new Car();
            Car theCar2
=new Car("吉利",300);
            GaoJiCar theCar3
=new GaoJiCar();

        }
    }
}

namespace Traffic
{
    
public class Vehicle
    {
        
private string name;
        
private int speed;
        
public Vehicle(){}
        
public Vehicle(string name)
        {
            
this.name=name;
        }
        
public Vehicle(string name,int speed)
        {
            
this.name=name;
            
this.speed=speed;
            Console.WriteLine(
"調用了父類中的兩個參數的構造函數.");
        }
        
public void getName()//成員方法
        {
            Console.WriteLine(
this.name);
        }
    }
    
public class Car:Vehicle
    {
        
public Car()
        {
            
base.getName();//使用父類中的成員方法
        }
        
//base(name,200)中的name是直接傳入的Car(string name)中的name
        
//因此這里在name前不需要有數據類型
        
//這里會先執行父類的再執行子類的構造函數
        public Car(string name,int speed):base(name,speed)
        {
            Console.WriteLine(
"子類中的構造函數");
        }
    }
    
public class GaoJiCar:Car
    {
        
//這里會依次執行Vehicle,Car,GaoJiCar都帶有兩個參數的構造函數
        public GaoJiCar():base("長安",124)
        {
            Console.WriteLine(
"子類的子類中的構造函數");
        }
    }
}

申明

非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

博文欢迎转载,但请给出原文连接。

原文地址:https://www.cnblogs.com/Athrun/p/1285155.html