class Program
{
static void Main(string[] args)
{
BasketBall basket = new BasketBall(5);
//basket.showFulei();
basket.Playing(3);
basket.Playing("男");
TableTennis table = new TableTennis();
YumaoBall yumao = new YumaoBall();
Console.ReadKey();
}
}
class Ball
{
private int r;
private string colour = "红色";
private string speed = "2m/s";
public string Speed
{
get { return speed; }
set { speed = value; }
}
public string Colour
{
get { return colour; }
set { colour = value; }
}
public int R
{
get { return r; }
set
{
if (value > 0)
{
r = value;
}
else
{
Console.WriteLine("输入半径不能小于零!");
}
}
}
public Ball()
{
Console.WriteLine("球类不带参的构造函数!");
}
public Ball(int _r)
{
r = _r;
Console.WriteLine("球类带参的构造函数!");
}
public void Mainji()
{
Console.WriteLine("面积是{0}", 4 * Math.PI * r * r);
}
public void Tiji()
{
Console.WriteLine("体积是{0}", 4 / 3 * Math.PI * R * R);
}
//----------虚函数-重写--------//注意重写跟重载的方法区别 写在父类和子类中
//父类 virtual 虚方法
//子类 override
public virtual void Moving(string speed)
{
Console.WriteLine("父类的移动方式:滚!");
}
//覆盖:写在父类和子类中 使用关键字new 返回类型相同 方法同名
public new void ZuoYong()
{
Console.WriteLine("父类-球的作用效果!");
}
}
class BasketBall:Ball
{
public BasketBall()
{
Console.WriteLine("子类不带参的构造函数!");
}
public BasketBall(int _r)
: base(_r)
{
if (_r > 0)
{
this.R = _r;
}
else
{
Console.WriteLine("输入半径不能小于零!");
}
Console.WriteLine("子类带参的构造函数!");
}
public void showFulei()
{
base.Tiji();
Console.WriteLine(base.Colour);
}
//----------------------------重载-------------------
//方法名一样 传参的类型不一样 与返回值无关- 在子类使用-------------------
public void Playing(int age) {
if (age<5)
{
Console.WriteLine("用脚踢!");
}
}
public void Playing(string sex)
{
if (sex == "男")
{
Console.WriteLine("用手打!");
}
}
}
class TableTennis:Ball
{
public TableTennis()
{
// this.Moving();
this.ZuoYong();
}
//重写是 返回类型一样 方法名必须一样 参数一样 加上关键修饰符
public override void Moving(string str1)
{
Console.WriteLine("冰瓶子类球移动方式为弹!");
}
public new void ZuoYong()
{
Console.WriteLine("子类-兵乓球的作用效果!");
}
}
class YumaoBall:Ball
{
public YumaoBall()
{
// this.Moving();
}
//重写是 方法名必须一样 参数一样
public override void Moving(string s1)
{
Console.WriteLine("羽毛球子类球移动方式为抛!");
}
protected new void ZuoYong()
{
Console.WriteLine("子类-羽毛球的作用效果!");
}
}