0
我想在C#中爲我已經擁有的一段代碼做一個等效代碼。 Java代碼如下。從C子類中訪問父類的方法#
class Test
{
public static void main (String args[])
{
C1 o1 = new C1();
C2 o2 = new C2();
System.out.println(o1.m1() + " " + o1.m2() + " " + o2.m1() + " " + o2.m2());
}
}
class C1
{
void C1() {}
int m1() { return 1; }
int m2() { return m1(); }
}
class C2 extends C1
{
void C2() {}
int m1() { return 2; }
}
這給出了輸出1 1 2 2.現在,我已經編寫了C#代碼。
class Program
{
static void Main(string[] args)
{
C1 o1 = new C1();
C2 o2 = new C2();
Console.WriteLine(o1.M1()+ " "+ o1.M2()+ " "+ o2.M1()+ " "+ ((C2)o2).M2()+ "\n");
Console.ReadKey();
}
}
public class C1
{
public C1()
{
}
public int M1()
{
return 1;
}
public int M2()
{
return M1();
}
}
public class C2:C1
{
public C2()
{
}
public int M1()
{
return 2;
}
}
然而,這印刷品1 1 2 1 如C2繼承M2調用C1 M1和不是M1在C2。如何在C2中調用M1並稍作修改?
https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx – Orangesandlemons