有一句看不太懂!

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:14:41
using System;
class TestClass
{
public class Dimensions
{
public const double PI = Math.PI;
protected double x, y;
public Dimensions()
{
}
public Dimensions(double x, double y)
{
this.x = x;
this.y = y;
}

public virtual double Area()
{
return x * y;
}
}

class Cylinder : Dimensions
{
public Cylinder(double r, double h) : base(r, h)
{
}

public override double Area()
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}

static void Main()
{
double r = 3.0, h = 5.0;
Dimensions l = new Cylinder(r, h);
Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());

我把他们的回答总结一句通俗的话吧:base关键字可以用来访问当前对象的基类对象,进而调用基类对象的成员!

还是举个例子吧:
比如说有个父类(基类)
public class Contact
{
//字段..
//属性..
//索引函数..

//构造函数
public Contact(string sName)
{

m_name=sName;

}
//方法..
}
现在有个子类(派生类)去继承调用他的基类对象的成员
则必须使用关键字base来实现,就象你是爸的儿子,你要继承父业的某些东西,你得和他姓!好象这个比喻有点牵强!
public class Classmate:Contact
{

//字段..
//属性..

//构造函数

public class Classmate(string sName):base(sName)
{

}

}

*************************************************
base(r, h) 调用父类的构造函数,然后this.x = x 是说,当前父类的x就变成传进来的r?,然后public Cylinder(double r, double h) : base(r, h)的时候就相当是继承了父类的构造函数的参数。其实父类的那个方法也不是很理解,顺便解释一下吧!
public Dimensions(double x, double y)
{
this.x = x;
this.y = y;
}
************************************************************
首先要知道 Base 是子类中引用父类
在这里因为是要将参数的x