C# Abstract抽象关键字示例

using System;

namespace ProgramCall

{

 //Abstract class

    abstract class Shape1

    {

       protected float R, L, B;

        //Abstract methods can have only declarations

       //cannot have method body

        public abstract float Area();

        public abstract float Circumference();

    }

   class Rectangle1 : Shape1

    {

        public void GetLB()

        {

            Console.Write("Enter  Length  :  ");

 

            L = float.Parse(Console.ReadLine());

            Console.Write("Enter Breadth : ");

            B = float.Parse(Console.ReadLine());

        }

       //must be override all the abstract method

      public override float Area()

        {

            return L * B;

        }

     public override float Circumference()

        {

            return 2 * (L + B);

        }

    }

     class MainClass

    {

        //Class containing abstract method cannot be instantiated

        public static void Calculate(Shape1 S)

        {

            Console.WriteLine("Area : {0}", S.Area());

            Console.WriteLine("Circumference : {0}", S.Circumference());

        }

        static void Main()

        {

            Rectangle1 R = new Rectangle1();

            R.GetLB();

            Calculate(R);

            Console.WriteLine();

            Console.Read();

 

        }

    }

}

C# virtual 关键字

  • 虚拟方法具有实现,其派生类不必再次实现它(但可以替换原始实现)。
  • 虚拟方法可以具有方法主体。
  • 如果我们觉得派生类可能或者可能不会覆盖基类方法,那么我们将将基类方法定义为虚拟。
  • 虚拟方法不需要强制覆盖。
  • 可以实例化包含虚拟方法的类。
  • 使用虚拟修改程序时存在限制。我们无法使用此修改程序以及静态或者抽象或者覆盖修饰符。

C# virtual关键字示例

using System;

namespace ProgramCall

{

    class Shape

    {

        protected float R, L, B;

        //A virtual method has an implementation

        //Virtual method can have a method body

        public virtual float Area()

        {

            return 3.14F * R * R;

        }

        public virtual float Circumference()

        {

            return 2 * 3.14F * R;

        }

        class Rectangle : Shape

        {

            public void GetLB()

            {

                Console.Write("Enter  Length  :  ");

                L = float.Parse(Console.ReadLine());

                Console.Write("Enter Breadth : ");

                B = float.Parse(Console.ReadLine());

            }

            public override float Area()

            {

                return L * B;

            }

            public override float Circumference()

            {

                return 2 * (L + B);

            }

            static void Main()

            {

                //Class containing virtual method can be instantiated

                Shape s = new Shape();

                s.Area();

                Rectangle R = new Rectangle();

                R.GetLB();

                Console.WriteLine("Area : {0}", R.Area());

                Console.WriteLine("Circumference : {0}", R.Circumference());

                Console.Read();

            }

        }

    }

}

C# Abstract

  • 一个抽象方法没有实现,它的派生类必须实施它。
  • 抽象方法只有签名。它不能有方法体。
  • 如果要强制执行派生类必须覆盖基类方法,则将基类方法定义为摘要。
  • 抽象的方法应该被派生类覆盖。
  • 包含抽象方法的类无法实例化。它只能继承。
  • 摘要修饰符也有限制。它们不能与静态或者虚拟或者覆盖修饰符一起使用。
C#中Abstract关键字和Virtual关键字有什么区别

在C#中Abstract和Virtual关键字之间的区别?

Virtual和Abstract方法都用于允许派生类覆盖基类的方法,但具有一些差异,在本文中,我们将解释C#中的虚拟和抽象关键字之间的一些重要区别。

日期:2020-04-11 22:50:26 来源:oir作者:oir