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

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

基础:

抽象类 AbstractClass

抽象类是一种特殊的基础类,并不与具体的事物联系。

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

转载于:https://www.cnblogs.com/PaulTsao/p/11486120.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> using namespace std; class CShape{ public:float Area; float Perimeter; CShape() { Area=0;Perimeter=0; } virtual void GetArea(){} virtual void GetPerimeter(){} }; class CRectangle:public CShape{ private: int l;int h; public: CRectangle(int h,int l ):CShape() {this->h=h;this->l=l; } void GetArea() {Area=l*h;cout<<"矩形的面积="<<Area<<endl;} void GetPerimeter() {Perimeter=(l+h)*2;cout<<"矩形的周长="<<Perimeter<<endl; } }; class CCirle:public CShape{ private:float r; public:CCirle(float r):CShape() {this->r=r; } void GetArea() { Area=3.14159*r*r; cout<<"圆的面积="<<Area<<endl; } void GetPerimeter() { Perimeter=r*3.14159*2; cout<<"圆的周长="<<Perimeter<<endl; } }; class CSquare:public CShape{ private: int a; public: CSquare(int a):CShape() { this->a=a; } void GetArea() { Area=a*a; cout<<"正方形面积="<<Area<<endl; } void GetPerimeter() { Perimeter=4*a; } }; class CTrapeziod:public CShape{ private: int lu; int ld; int h; public: CTrapeziod(int lu,int ld,int h):CShape() { this->lu=lu; this->ld=ld; this->h=h; } void GetArea() { Area=(lu+ld)*h/2; cout<<"梯形面积="<<Area<<endl; } }; class CTrangle:public CShape{ private: int d; int h; public: CTrangle(int d,int h):CShape() { this->d=d; this->h=h; } void GetArea() { Area=d*h/2; cout<<"三角形的面积="<<Area<<endl; } }; void main() { CShape *p[5]; CRectangle CR(5,10); CCirle CC(3); CSquare CS(4); CTrapeziod CT1(2,5,4); CTrangle CT2(6,2); p[0]=&CR; p[0]->GetArea(); p[1]=&CC; p[1]->GetArea(); p[2]=&CS; p[2]->GetArea(); p[3]=&CT1; p[3]->GetArea(); p[4]=&CT2; p[4]->GetArea(); int i, double SumArea=0.f; for(i=0;i<5;i++) { SumArea=p[i]->Area+SumArea; } cout<<"面积总和="<<SumArea<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值