class Program
{
static void Main(string[] args)
{
RoundClass round = new RoundClass(2);//實例化子類函數(shù)時端逼,先執(zhí)行父類構(gòu)造函數(shù)奈附,再執(zhí)行子類的構(gòu)造函數(shù)牲迫;
round.show();
SphereClass sphere = new SphereClass();
sphere.d; //通過實例化也調(diào)用不了d,因為d是受保護的,只能給父類和子類使用;要調(diào)用d必須改權(quán)限
round.a// 點不出來,a在父類是私有的缤至;
//練習
SphereClass sph = new SphereClass();
RoundClass rou = new RoundClass();
rou.ShowDate();
Console.ReadKey();
}
}
class RoundClass : SphereClass//子類:父類
{
public RoundClass(int rr)
: base("1")
{//調(diào)用父類帶參構(gòu)造函數(shù);// 子類構(gòu)造函數(shù)(形參) base(實參)
Console.WriteLine("這是子類的構(gòu)造函數(shù)康谆!");
}
public void ShowDate()
{
Console.WriteLine("這是子類的普通方法凄杯!");
}
public void show()
{
Console.WriteLine("半徑是:{0}直徑是:{1}", r, d);//子類直接調(diào)用父類的屬性或 字段
this.ShowSphere();
this.ShowDate();
base.ShowDate();
}
public RoundClass()
: base()
{
Console.WriteLine("這是子類的不帶參函數(shù)!");
}
public RoundClass(int rr)
: base(rr)
{
this.R = rr;
Console.WriteLine("這是子類的帶參函數(shù)秉宿!");
}
public void Show()
{
Console.WriteLine("這是子類的普通方法");
}
public void ShowDate()
{
this.ShowSphere();
this.Show();
base.Show();
}
}
class SphereClass
{
public int r;
//private int a;
protected int d;
public int R
{//屬性
set { r = value; }
get { return r; }
}
public SphereClass()
{//構(gòu)造函數(shù)初始化戒突;
r = 5;
d = 2 * this.r;
Console.WriteLine("這是不帶參父類的構(gòu)造函數(shù)!");
}
public SphereClass(int _r)
{//構(gòu)造函數(shù)初始化描睦;父類帶參構(gòu)造函數(shù)
// r = _r;
Console.WriteLine("這是帶參父類的構(gòu)造函數(shù)膊存!");
}
public void ShowDate()
{
Console.WriteLine("這是父類的普通方法!");
}
public void ShowSphere()
{
Console.WriteLine("半徑是:{0}直徑是:{1}", this.R, d);
}
private int r;
protected int d;
public int R
{
set { r = value; }
get { return r; }
}
public SphereClass()
{
r = 5;
d = 2 * r;
Console.WriteLine("這是父類不帶參構(gòu)造函數(shù)!");
}
public SphereClass(int _r)
{
r = _r;
d = 2 * r;
Console.WriteLine("這是父類帶參的函數(shù)!");
}
public void Show()
{
Console.WriteLine("這是父類的普通方法隔崎!");
}
public void ShowSphere()
{
Console.WriteLine("半徑{0} 直徑{1}", r, d);
}
}