代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class 繼承1
{
//virtual虛函數,這樣寫便於子類重寫
public virtual double Area(double a)
{
return a * 2;
}
}
class Test : 繼承1
{
//重寫父類的Area,當然也可以調用父類的Area方法
public override double Area(double b)
{
//關鍵字base.Area()調用了父類的Area方法
return base.Area(b) * 0.9;
}
}
class Result
{
static void Main()
{
Test TT = new Test();
Console.WriteLine(TT.Area(10));
}
}
}
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Class4
{
//申明2個構造函數
public Class4()
{
Console.WriteLine("Class4");
}
int i;
public Class4(int a)
{
i=a;
Console.WriteLine("Class:{0}", i);
}
public void SS()
{
Console.WriteLine("BOSS");
}
}
class Test:Class4
{
//調用父類(無參數的)構造函數
public Test()
: base()
{
}
//調用父類(有參數的)構造函數
public Test(int a)
: base(a)
{
base.SS();
}
}
class TT
{
static void Main()
{
Test T = new Test();
Test T1 = new Test(10);
}
}
}