class Program
{
static void Main(string[] args)
{
//定义一个球类,利用封装的特性,封装出球类的特征与行为,同时给其定义多个构造方法;
//再定义一个篮球类,继承球类,给篮球类定义多个构造方法,
//在篮球类中显示调用父类的构造方法,以及自己的其他构造方法,
//在篮球类的ShowFulei方法里面调用父类的某一个属性,以及父类的体积方法,设计此程序�;
Sphere sph = new Sphere();//实例化球体,不带参
Sphere sph1 = new Sphere(6);//实例化球体,带参;
Basketball basket = new Basketball();//实例化篮球,不带参 首先调用父类不带参
Basketball basket1 = new Basketball("乔丹篮球");//实例化篮球,带参 首先调用父类带参
basket.ShowFulei(10);//调用计算体积的方法,传参去修改父类属性;
basket.Show();//显示同名方法
Console.ReadKey();
}
}
class Sphere
{
//定义一个球类,利用封装的特性,封装出球类的特征与行为,
//同时给其定义多个构造方法;
private string name;
private double weight;
private double r;
private double perimeter;
private double area;
private double volume;
public double R {
set { if (value > 0)
{
r = value;
}
else {
r = 0;
} }
get { return r; }
}
public string Name
{
set
{
name = value;
}
get { return name; }
}
public double Weight
{
set
{
if (value > 0)
{
weight = value;
}
else
{
weight = 0;
}
}
get { return weight; }
}
public void Roll() {
Console.WriteLine(Name +"滚动起来了!");
}
public void Beat() {
Console.WriteLine(Name+"被拍打!");
}
public Sphere() {
R = 5;
perimeter = 2 * Math.PI * R;
Console.WriteLine("这是不传参的父类构造函数!\n计算周长:{0}\n",perimeter);
}
public Sphere(double _r) {
this.R = _r;
area = Math.PI * R * R;
Console.WriteLine("这是带参的父类构造函数!\n计算面积:{0}\n",area);
}
public double SphereVolume() {//计算球体体积的方法
volume = Math.PI * R * R * R;
return volume;
}
public void ShowBall() {
Console.WriteLine("这个球是父类显示同名方法!");
}
}
class Basketball:Sphere
{//再定义一个篮球类,继承球类,给篮球类定义多个构造方法,
//在篮球类中显示调用父类的构造方法,以及自己的其他构造方法,
//在篮球类的ShowFulei方法里面调用父类的某一个属性,以及父类的体积方法,设计此程序�;
public Basketball():base(1) {
this.Name = "";
this.R = 5;
this.Weight = 80;
Console.WriteLine("这是子类的不带参的构造函数!");
}
public Basketball(string _name):base(6) {
this.Name = _name;
this.Roll();//调用父类的函数;
Console.WriteLine("这是子类的带参构造函数!");
}
public void ShowFulei(double r_) {
this.R = r_;
double volume= this.SphereVolume();
Console.WriteLine("体积是:{0}\n",volume);
}
public void ShowBall() {
Console.WriteLine("这个球是子类显示同名方法!");
}
public void Show()
{
Console.WriteLine("以下是调用父类\\子类的显示方法!");
this.ShowBall();
base.ShowBall();
}
}
}