msdn考过来的 示例 本示例显示如何指定在创建派生类实例时调用的基类构造函数。// keywords_base2.cs using System; public class MyBase { int num; public MyBase() { Console.WriteLine("in MyBase()"); } public MyBase(int i ) { num = i; Console.WriteLine("in MyBase(int i)"); } public int GetNum() { return num; } }public class MyDerived: MyBase { // This constructor will call MyBase.MyBase() public MyDerived() : base() { } // This constructor will call MyBase.MyBase(int i) public MyDerived(int i) : base(i) { } public static void Main() { MyDerived md = new MyDerived(); MyDerived md1 = new MyDerived(1); } } 输出 in MyBase() in MyBase(int i)
{
public void m(){};
}class B : A
{
public void m(){}//隐藏A类的m() ....
this.m(); // B.m();
base.m(); // A.m();
}
{
[STAThread]
static void Main(string[] args)
{
D d = new D("base","public ","protected");
}
}
public class C
{
public string str0;
protected string str1;
private string str2;
public C(string str)
{
this.str2 = str;
System.Console.WriteLine(str2);
}
}
public class D:C
{
public D(string str2,string str0,string str1):base(str2)
{
this.str0 = str0;
this.str1 = str1;
System.Console.WriteLine( str0 + str1 );
}
}
我贴一个吧,这是会员培训时给师弟们讲public,protected,private时给师弟讲的例子,本想连base也一起讲了,但怕自己了解的不够透
就是调用
类C的
public C(string str)
{
this.str2 = str;
System.Console.WriteLine(str2);
}
示例
本示例显示如何指定在创建派生类实例时调用的基类构造函数。// keywords_base2.cs
using System;
public class MyBase
{
int num; public MyBase()
{
Console.WriteLine("in MyBase()");
} public MyBase(int i )
{
num = i;
Console.WriteLine("in MyBase(int i)");
} public int GetNum()
{
return num;
}
}public class MyDerived: MyBase
{
// This constructor will call MyBase.MyBase()
public MyDerived() : base()
{
} // This constructor will call MyBase.MyBase(int i)
public MyDerived(int i) : base(i)
{
} public static void Main()
{
MyDerived md = new MyDerived();
MyDerived md1 = new MyDerived(1);
}
}
输出
in MyBase()
in MyBase(int i)