Dll文件

记得我在N年前学会装软件的时候,就知道文件中有一个叫dll的文件,发现他很重要,一个程序如果删除了它,程序就根本运行不了。
什么是DLL呢?
   它就是动态链接库。动态链接库是一种程序模块。它的组成有:一、可执行代码 二、各种类型的预定义的数据和资源。
那它有什么作用呢?
  一、常常用于开发大型软件系统。一个小程序可以把所有的代码写在一个文件,但是一个大型系统也这样,那程序就太庞大了,而且还可能有很多重复的功能,如果利用由顶之下的功能分解原理把程序分解为一系列主程序与动态链接库组成,这就大大减少了工作量,而且更容易管理。
  二、软件产品国际化。我想大家都有这样的经历,当装一个软件时,他总是会提示你选择语言。这种功能就是动态链接库来实现的,他有多种语言的动态链接库,根据你的选择从而加载相应的DLL程序。

大家也许会说,讲个DLL有什么用?大家不妨想一下现在程序的三层模式:
    一、表示层(与用户打交道的部分)
 二、商业逻辑层(根据用户输入的数据OR操作,执行相应的操作)
 三、数据层
数据层通常是一些数据库管理系统,我们可以不必理会。表示层是与用户打交道的部分,一般不会太难。商业逻辑层才是我们经常要编的一个层次。因为商业逻辑层要实现的功能很多,所以我们就利用DLL的思想按功能分成不同的模块,表示层运行时需要什么功能,就装载什么DLL。这样即可提高运行效率,又方便了编程。更是开发大型程序成为了可能。

现在我们就用C#编一个小游戏来说明,如果用C#构造一个完整的控制台应用程序。
程序功能:
    程序开始时,显示一下程序的开始信息,然后根据用户的操作是玩游戏还是退出,如果玩要求用户输入下的注,然后与随机产生的图形(直角三角形,等腰直角三角形、矩形与正方形)相对应,根据图形的面积加相应的分数。每赌注一轮,分四次。最后计算剩下的分数。
至于功能分解,我就不写了。简要说一下在这个程序中运用了C#中的哪些知识,如果看了我程序中的注解还不了解,大家可以查一下专门的有关C#讲解的书吧.
知识点:
      一、利用namespace将程序划分为多个功能模块,装配为DLL组件
      二、利用类的多态性,得同一个操作产生不同的执行效果(比方画图形、求面积)
  三、再就异常处理
  四、C#的一些基本语法
自己创建的namespace:
    一、MyShape 编译后MyShape.DLL组件,用于图形的绘制与面积的求解
    二、MyMessage   编译后为MyMessage组件,用于程序开始时显示的一些基本信息和根据用户的输入判断是继承玩还是退出
    三、用户界面程序,(要用到上面的两个组件)
类的关系:
  
      基类(图形Shape)---|---矩形---正方形
           |--三角形--直角三角形--等腰直解三角形
  
类的方法:
    一、画该类的图形
    二、求图形的面积
现在给出源代码吧:
文件一client.cs:客户程序

using   System;
using   MyShape;
using   MyMessage;

class   Play
{
static   void   Main()
{
//定义用户开始的一些基本信息
int   score   =1000;//总分
int   win=0;//每一局胜利得到的分数 
int   choice   ;//随机获得的图形号
int   bet;//每一局下的注
string   s;
Shape   sp=new   Shape   ();
Random   ran=new   Random   ();
Message   msg=new   Message   ();
msg.Begin   ();
while(true)
{
if(!msg.Ask   ())
{
break;
}
Console.WriteLine   ( "你这轮开始的分数为:{0} ",score);
Console.WriteLine   ( "下注: ");
s=Console.ReadLine   ();
//利用异常处理,并默认为100
try  
{
bet=Int32.Parse   (s);
}
catch
{
bet=100;
Console.WriteLine   ( "你输入的不符合,默认为100分! ");
}
//将用户的总分减去用户赌注的分数
if(bet <score)
{       score-=bet;}
else
{
bet=score;
score=0;
}
choice=ran.Next   ()%4;
switch   (choice)
{
case   0:
sp=new   RectTriangle   (5,4);break;
case   1:
sp=new   RectEqualTriangle   (5);break;

case   2:
sp=new   Rectangle   (5,4);break;

case   3:
sp=new   Square   (5);break;

}
//利用多态性,计算得分
Console.WriteLine   ( "你的幸运图形为: ");
sp.Draw   ();
win+=sp.GetArea   ()*5*bet/100;
Console.WriteLine   ( "本轮获得:{0}分 ",win);
score+=win;
Console.WriteLine( "你现在的分数为:{0} ",score);
if(score <100)
{
Console.WriteLine   ( "你没有足够的分数继续玩! ");
}

}
}
}

文件二 MyMessage.cs:程序中要输出的一些消息
using   System;

namespace   MyMessage     //用来定义程序显示的一些信息
{
public   class   Message
{       int   i;
public   void   Begin()     //定义一个开始方法,用来显示一些信息
{
Console.WriteLine( "游戏开始! ");
}

public   bool   Ask()     //根据用户的操作,决定是玩还是退出游戏
{
Console.WriteLine   ( "按零退出游戏 ");
Console.WriteLine   ( "按别的键开始: ");
Console.WriteLine   ();
string     s=Console.ReadLine   ();
try
{
  i=Int32.Parse   (s);
}
catch
{
  i=59;
}

if(i!=0)
{       Console.WriteLine   ( "你已经退出游戏 ");
return   true;
}
else
{
return   false;
}

}

}



}

文件三   shape.cs/自定义的图形基类
sing   System;

namespace   MyShape
{

public   class   Shape   //定义一个图形的基类,用来绘制图形和计算机图形的面积
{
public     virtual   void   Draw()
  {;}     //虚方法,用于图形的绘制
public   virtual   int   GetArea()
{return   0;}       //虚方法,用于计算机图形的面积
}
}

文件四、正方形与矩形类   rect.cs
using   System;

namespace   MyShape     //用于定义矩形类和正方形类
{
public   class   Rectangle:Shape     //定义矩形类,从基本的图形类来继承
{
    //1.定义矩形类的两个数据成员:长和宽   a,b
    protected   int   a;
    protected   int   b;

    //2.初始化数据成员---利用构造函数
public   Rectangle(int   va,int   vb)
{
                        a=va;
b=vb;
}

  //3.利用重载虚方法,在屏幕上绘制矩形
public   override   void   Draw()  
{
Console.WriteLine   ( "矩形: ");
Console.WriteLine   ( "------------- ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "------------- ");
}
  //4.重载虚方法GetArea(),计算图形面积
public   override   int   GetArea()
{
int   area=a*b;
return   area;
}
}


public   class   Square:Rectangle     //定义正方形类,从矩形类中继承过来的
{
//利用构造函数,初始化数据成员
public   Square(int   va):base(va,va)
{;}

//重载Draw(),绘制正方形
public   override   void   Draw()  
{
Console.WriteLine   ( "正方形: ");
Console.WriteLine   ( "------------- ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "|                         | ");
Console.WriteLine   ( "------------- ");
}

}
}
文件五、三角形类   triangle.cs
using   System;
using   MyShape;

namespace     MyShape
{
//定义普通的三角形,从基本的图形中继承
public   class   Triangle:Shape
{
//定义三角的数据成员,三边a,b,c
protected   int   a;
protected   int   b;
private   int   c;

//利用构造函数,初始化类
public   Triangle(int   va,int   vb,int   vc)
{
a=va;
b=vb;
c=vc;
}

//重载GetArea(),用来计算机图形的面积
public   override   int   GetArea()
{
int   s=(a+b+c)/2;
int   area=(int)(Math.Sqrt(s*(s-a)*(s-b)*(s-c)));
return   area;
}
}


//定义直角三角形
public   class   RectTriangle:Triangle
{       //盖掉数据成员,所以用new关键字
new   protected   int   a;
new   protected   int   b;

//初始化类
        public   RectTriangle(int   va,int   vb):base(va,vb,(int)(Math.Sqrt   (va*va+vb*vb)))
{
a=va;
b=vb;
}
//重载方法Draw(),用来计算图形的面积
public   override   void   Draw()
{
Console.WriteLine   ( "* ");
Console.WriteLine   ( "*   * ");
Console.WriteLine   ( "*       * ");
Console.WriteLine   ( "****** ");
}
//重载方法GetArea(),用来计算三角形的面积
public   override   int   GetArea()
{
int   area=(int)(a*b/2);
return   area;
}
}

//定义等腰直角三角形
public   class   RectEqualTriangle:RectTriangle
{       //定义数组成员
new   protected   int   a;
//利用构造函数,用来初始化类
public   RectEqualTriangle(int   va):base(va,va)
{
  a=va;
}
//重载方法Draw(),用来绘制图形
public   override   void   Draw()
{
Console.WriteLine   ( "* ");
Console.WriteLine   ( "*     * ");
Console.WriteLine   ( "*         * ");
Console.WriteLine   ( "******** ");
}

            //重载函数GetArea(),用来计算图形面积
public   override   int   GetArea()
{
int   area=(int)(a*a/2);
return   area;
}


}
}
作者:vagrant
oicq:2381758
联系E-MAIL:ljx197926@sina.com.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值