zoukankan      html  css  js  c++  java
  • C#进阶之路——9.C# 抽象类

    C#进阶之路——9.C# 抽象类

    基础:

    抽象类 Abstract Class

    抽象类理解为一种特殊的基础类,它不与具体的事物直接产生联系。

    形式上,用abstract关键字修饰的类即为抽象类。其最大的特点是不能被实例化,只能作为派生类的基类。

    1、一个抽象类可以同时包含抽象方法和非抽象方法。

    2、抽象方法只在派生类中真正实现,抽象类中的抽象方法只存放函数原型,不涉及主体代码,

    3、派生自抽象类的类需要实现其基类的抽象方法,才能实例化对象。

    4、使用override关键子可在派生类中实现抽象方法,经override声明的方法即为重写基类方法,其签名必须与override方法的签名相同。

    注意:抽象类不能被实例化,他只能作为其他类的基础类。比如,类的层次结构中并没有“图形”这样的具体事物,将“图形”定义为抽象类,便可以派生出“圆形”和“四边形”这样一些可以被具体实例化的普通类。

    可以在抽象类中使用关键字absract定义抽象方法,并要求所有的派生非抽象类都要重载实现抽象方法。引入抽象方法的原因在于抽象类本身是一种抽象概念,有的方法并不需要具体实现,而是留下来让派生类重载实现。 

    比如:图形抽象类和圆形和正方形具体类,以及计算面积抽象方法。

    public absract class shape

    {

    .....

    }

      抽象方法为:

      public absract double GetArea();

      则派生类重载实现为:

      public override double GetArea();

    {

    ......

    }

    进阶:

    示例:图形抽象类和圆形和正方形具体类,以及计算面积抽象方法重载实现。

    在工程文件中添加一个类 Shape类——Shape.cs

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Application27

    {

       //定义基类Shape

        public abstract class Shape

        {

            protected string Color;

            public Shape() { ;}   //构造函数

            public Shape(string Color) { this.Color = Color; }

            public string GetColor()  { return Color; }

            public abstract double GetArea();   //抽象类

        }

        //定义Cirle类,从Shape类中派生

        public class Circle : Shape

        {

            private double Radius;

            public Circle(string Color, double Radius)

            {

                this.Color = Color;

                this.Radius = Radius;

            }

            public override double GetArea()

            {

                return System.Math.PI * Radius * Radius;

            }

       

        }

        //派生类Rectangular,从Shape类中派生

        public class Retangular : Shape

        {

            protected double Length, Width;

            public Retangular(string Color, double Length, double Width)

            {

                this.Color = Color;

                this.Length = Length;

                this.Width = Width;

            }

            public override double GetArea()

            {

                return (Length*Width);

            }

            public double PerimeterIs()

            {

                return (2 * (Length * Width));

           

            }

        }

        //派生类Square,从Retangular类中派生

        public class Square : Retangular

        {

            public Square(string Color,double Side):base(Color,Side,Side) { ;}

        

       

        }

    }

    在主程序中设置参数并调用执行——Program.cs

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Application27

    {

        class Program

        {

            static void Main(string[] args)

            {

                 Circle Cir = new Circle("Orange", 3.0);

                Console.WriteLine("Circle area is{1}",Cir.GetColor(),Cir.GetArea());

                Retangular Rect = new Retangular("Red",13.0,2.0);

                Console.WriteLine("Retangular Color is {0},Rectangualr area is {1},Rectangualr Perimeter is {2}",

                    Rect.GetColor(),Rect.GetArea(),Rect.PerimeterIs());

               Square Squ = new Square("qreen",5.0);

                Console.WriteLine("Square Color is {0},Square Area is {1},Square Perimeter is {2}",Squ.GetColor(),Squ.GetArea(),Squ.PerimeterIs());

             }

        }

    }

    控制台输出

     

    来源:

    https://www.cnblogs.com/CCMMBN/p/8719319.html

    转载引用请注明出处。 文章作者:PaulTsao 文章来源:http://www.cnblogs.com/PaulTsao/ 作者信息:www.linkedin.com/apollocaoy
  • 相关阅读:
    二叉树的建立和遍历
    canvas基础入门(一)canvas的width、height于css样式中的宽高区别
    js实现放大镜效果
    ios中页面底部输入框,position:fixed元素的问题
    js input复选框选中父级同时子级也选中
    js apply和call
    javascript闭包理解
    vue.js vue-jsonp解决跨域问题
    vue.js请求数据(axios)
    vuex
  • 原文地址:https://www.cnblogs.com/PaulTsao/p/11486120.html
Copyright © 2011-2022 走看看