zoukankan      html  css  js  c++  java
  • C#基础--Virtual与abstract区别、重写

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

     1  public class Animal
     2     {
     3         public string Name { get; set; }
     4         public virtual void Eat()
     5         {
     6             Console.WriteLine("{0}正在吃草",Name);
     7         }
     8 
     9     }
    10     public class Sheep : Animal
    11     {
    12         public Sheep(){ Name = ""; }
    13         public override void Eat()
    14         {
    15             base.Eat();
    16             Console.WriteLine("吃草");
    17 
    18         }
    19 
    20     }
    21 
    22     public class Tigger : Animal
    23     {
    24         public Tigger() { Name = "老虎"; }
    25         public override void Eat()
    26         {
    27             base.Eat();
    28             Console.WriteLine("老虎吃羊");
    29 
    30         }
    31     }
       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());
    

      

  • 相关阅读:
    java生成随机大数据文件
    spark读hdfs文件实现wordcount并将结果存回hdfs
    spark streaming 实现接收网络传输数据进行WordCount功能
    spark mllib k-means算法实现
    java实现读取文件大全
    spark+hcatalog操作hive表及其数据
    在hdfs上存取xml文件的实现代码
    hadoop shell 详解
    day 01 预科
    c盘没有新建修改权限的,执行下面命令
  • 原文地址:https://www.cnblogs.com/QueryWord/p/11407124.html
Copyright © 2011-2022 走看看