虚函数简单示例
2007-07-15 11:52
232 查看
虚函数简单地说,那些被virtual关键字修饰的成员函数。虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性就是实现共同的方法,但因个体差异而采用不同的策略。
简单的示例
public class ShapeWithArea
...{
protected double x,y,area;
protected string name;
public ShapeWithArea(double x,double y)
...{
this.x=x;
this.y=y;
}
public virtual double Area()
...{
area= x*y;
return area;
}
public override string ToString()
...{
return name+"的面积是:"+Area().ToString();
}
}
public class Circle:ShapeWithArea
...{
public Circle(int r):base(r,0)
...{
base.name="圆";
}
public override double Area()
...{
return Math.PI*x*x;
}
}
public class Sphere:ShapeWithArea
...{
public Sphere(int r):base(r,0)
...{
base.name="球";
}
public override double Area()
...{
return 4*Math.PI*x*x;
}
}
public class Cylinder:ShapeWithArea
...{
public Cylinder(int r,int h):base(r,h)
...{
base.name="圆柱";
}
public override double Area()
...{
return 2*Math.PI*x*x+2*Math.PI*x*y;
}
}
//如何调用
txtinfo.Text=string.Empty;
ShapeWithArea[] test=new ShapeWithArea[3];
test[0]=new Circle(Int32.Parse(txtcircle.Text));
test[1]=new Sphere(Int32.Parse(txtsphere.Text));
test[2]=new Cylinder(Int32.Parse(txtcylinderr.Text),Int32.Parse(txtcylinderh.Text));
string info=string.Empty;
foreach(ShapeWithArea swa in test)
...{
info+=swa.ToString();
}
txtinfo.Text=info;
http://dl2.csdn.net/down4/20070715/15115143803.rar
示例工程文件
简单的示例
public class ShapeWithArea
...{
protected double x,y,area;
protected string name;
public ShapeWithArea(double x,double y)
...{
this.x=x;
this.y=y;
}
public virtual double Area()
...{
area= x*y;
return area;
}
public override string ToString()
...{
return name+"的面积是:"+Area().ToString();
}
}
public class Circle:ShapeWithArea
...{
public Circle(int r):base(r,0)
...{
base.name="圆";
}
public override double Area()
...{
return Math.PI*x*x;
}
}
public class Sphere:ShapeWithArea
...{
public Sphere(int r):base(r,0)
...{
base.name="球";
}
public override double Area()
...{
return 4*Math.PI*x*x;
}
}
public class Cylinder:ShapeWithArea
...{
public Cylinder(int r,int h):base(r,h)
...{
base.name="圆柱";
}
public override double Area()
...{
return 2*Math.PI*x*x+2*Math.PI*x*y;
}
}
//如何调用
txtinfo.Text=string.Empty;
ShapeWithArea[] test=new ShapeWithArea[3];
test[0]=new Circle(Int32.Parse(txtcircle.Text));
test[1]=new Sphere(Int32.Parse(txtsphere.Text));
test[2]=new Cylinder(Int32.Parse(txtcylinderr.Text),Int32.Parse(txtcylinderh.Text));
string info=string.Empty;
foreach(ShapeWithArea swa in test)
...{
info+=swa.ToString();
}
txtinfo.Text=info;
http://dl2.csdn.net/down4/20070715/15115143803.rar
示例工程文件
相关文章推荐
- zz MATLAB 与 C 语言混合编程接口:MEX 函数最简单示例之一
- VC6.0配置LUA编译环境及LUA调用C++函数的简单示例
- Python中join函数简单代码示例
- php中preg_replace_callback函数简单用法示例
- string类 成员函数简单示例(1)
- 管道函数(pipelined function)简单使用示例
- VC6.0配置LUA编译环境及LUA调用C++函数的简单示例
- go语言函数参数与返回值示例,数组的简单应用
- php中preg_replace_callback函数简单用法示例
- string 类成员函数的简单示例(2)
- 管道函数(pipelined function)简单使用示例
- JAVA函数复写的简单示例
- JavaSE8基础 函数返回一个匿名对象 简单示例
- 简单的函数对象示例
- Python函数的默认参数和返回值的简单示例
- 代码函数从零开始学习OpenCL开发(二)一个最简单的示例与简单性能分析
- ORACLE自定义函数之简单示例
- 【Python-matplotlib】subplot2grid()函数的简单示例
- 用map函数来完成Python并行任务的简单示例
- oracle中函数和存储过程简单示例-动态sql