zoukankan      html  css  js  c++  java
  • 9 abstract 和 Virtual 之间的差别


    (1) abstract方法没有详细的实现。同一时候必须被覆写

    (2) 虚(Virtual)方法能够没有详细的实现,也不一定必须覆写(虚方法定义时,能够没有详细的实现代码,可是必须创建方法体:即必须有方法的左右花括号)。

    (3) 抽象方法不能够使用base.method()方式调用,可是虚方法是能够的


    //定义一个抽象方法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MVATwentyQuestions
    {
        abstract class absClass
        {
            public abstract void DisplayValue(string value);
        }
    }
    
    //在抽象类的子类中覆写抽象方法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MVATwentyQuestions
    {
        class absClassInherited : absClass
        {
            override public void DisplayValue(string value)
            {
                Console.WriteLine(value);
            }
        }
    }
    
    //定义一个虚方法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MVATwentyQuestions
    {
        class virtClass
        {
            public virtual void DisplayValue(string value)
            {
                Console.WriteLine(value);
            }
        }
    }
    
    //在子类中覆写虚方法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MVATwentyQuestions
    {
        class virtClassInherited : virtClass
        {
            public override void DisplayValue(string value)
            {
                Console.WriteLine(value.ToUpper());
            }
        }
    }


  • 相关阅读:
    Python—模块
    Python之路_Day5
    Python之路_Day4
    Py获取本机指定网卡的ip地址
    Python之路_Day3
    Python之路—Day2作业
    Python之路—Day2
    Python之路—Day1作业
    Python之路—Day1
    Python数据类型
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5133184.html
Copyright © 2011-2022 走看看