public class Father
{
public Father()
{
// // TODO: 在此處添加構造函數邏輯 //
}
public int a = 1;
public int b()
{
return this.a;
}
}
public class Son:Father{
public Son()
{
// // TODO: 在此處添加構造函數邏輯 //
}
public int a = 2;
}
調用:
Son s = new Son();
int r = s.b();
Console.WriteLine(r);
爲什麼是1不是2啊 我已經在sun中重寫了啊.
1. 只有方法的重載,沒有數據成員的重載,數據成員只有繼承關係。 2. 類的封閉性決定了類的方法只能訪問類自身的數據成員。 3. 要實現你的目的,對父類的方法C用virtual修飾,然後在子類中用override重寫。 |
class Program
{
static void Main(string[] args)
{
Son s = new Son();
int r = s.b();
Console.WriteLine(r);
Console.ReadKey();
}
}
public class Father
{
public Father()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public int a = 1;
public virtual int b()
{
return this.a;
}
}
public class Son : Father
{
public Son()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public int a = 2;
public override int b()
{
return this.a;
}
// public new int a = 2;
}