Virtual作用:子类可以对父类重写,虚方法是对多态特征体现。代表一类对象的所具有的公共属性或方法。

  public class Animal
{
public string Name { get; set; }
public virtual void Eat()
{
Console.WriteLine("{0}正在吃草",Name);
} }
public class Sheep : Animal
{
public Sheep(){ Name = "羊"; }
public override void Eat()
{
base.Eat();
Console.WriteLine("吃草"); } } public class Tigger : Animal
{
public Tigger() { Name = "老虎"; }
public override void Eat()
{
base.Eat();
Console.WriteLine("老虎吃羊"); }
}
   Animal animal1 = new Sheep();
Animal animal2 = new Tigger();
animal1.Eat();
animal2.Eat();

 abstract作用:子类必须对父类重写,虚方法是对多态特征体现。代表一类对象的所具有的公共属性或方法。

                             如果一个类中包含抽象方法,这个类必须是抽象类。

public abstract class Shape
{
public const double Pi = 3.14;
protected double x, y;
public Shape()
{
x = y = 0;
}
public Shape(double x,double y)
{
this.x = x;
this.y = y;
}
public abstract double Area(); }
//矩形
public class Rectangle : Shape
{ public Rectangle() : base() { }//使用基类的构造函数
public Rectangle(double x, double y) : base( x, y) { }
public override double Area()
{
return x * y;
}
}
//椭圆
public class Ellipase : Shape
{
public Ellipase(double x, double y) : base(x, y) { }
public override double Area()
{
return x * y * Pi;
}
}
//圆形
public class Round : Shape
{
public Round(double x) : base(x, 0) { }
public override double Area()
{
return x*x*Pi;
}
}

  

 double x = 2;
double y = 3;
Shape shape1 = new Rectangle(x,y);
Console.WriteLine("Rectangle:"+ shape1.Area());
Shape shape2= new Round(x);
Console.WriteLine("Round:"+ shape2.Area());
Shape shape3 = new Ellipase(x,y);
Console.WriteLine("Ellipase:"+shape3.Area());

  

最新文章

  1. (转载)JavaWeb学习总结(五十)——文件上传和下载
  2. java提高篇(九)-----实现多重继承
  3. mac版 android破解软件下载安装
  4. JSP中的隐式对象(implicit object)
  5. Windows7下 配置 Apache + PHP + MySQL + Zend Studio配置
  6. python-django 模型model字段类型说明
  7. 【linux】如何查看和解压缩rpm文件内容
  8. weblogic/utils/NestedException
  9. SRM 386(1-250pt)
  10. Python基础 1----Python语言基础和Python环境准备与安装
  11. 18 4Sum(寻找四个数之和为指定数的集合Medium)
  12. SHELL要发送HTML这类邮件的话,还得靠msmtp 和 mutt
  13. js判断值是否为数字
  14. 学习hadoop
  15. 结构-行为-样式-PS笔记
  16. Makefile:130: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
  17. node.js中express的Router路由的使用
  18. window 安装gdal和python
  19. ElementUI的提示框的使用记录
  20. Spring Cache 源码解析

热门文章

  1. 多线程-Thread和ThreadPool
  2. 使用requirejs+vue 打造 无需编译发布便捷修改调整的模块开发方案 (一)
  3. linux redis 设置密码:
  4. 123457123457#1#-----com.threeapp.circlerunner01----儿童旋转跑酷游戏
  5. ideal配置使用Git
  6. Java中四个作用域的可见范围
  7. Docker storage driver(十四)
  8. Turbine聚合https微服务
  9. 洛谷 题解 UVA1151 【买还是建 Buy or Build】
  10. C语言--函数嵌套调用