福建农林大学C#程序设计总复习材料 联系客服

发布时间 : 星期一 文章福建农林大学C#程序设计总复习材料更新完毕开始阅读382097a37c1cfad6185fa718

}

class B : A {

public static void Main(string[] args) {

//here } }

以下哪段代码可以位于here处 ( C ) A. Object a = new A(); a.Sleep(); B.B b = new A(); b.Sleep(); C. A c = new B(); c.Sleep(); D.Object b = new B(); b.Sleep(); 11、动态绑定。 【例5】请分析下面程序的输出结果。 using System; class A {

public virtual void Func1() { Console.WriteLine(\}

class B : A {

public override void Func1() { Console.WriteLine(\ public void Func2() { Console.WriteLine(\ } }

class C {

public static void CallA(A a) {

if (a is B) {

B b = (B)a; b.Func1(); b.Func2(); } else {

a.Func1(); } }

public static void Main(string[] args) {

A a = new A(); CallA(a); Console.WriteLine(a is A); }

} }

}

输出: A:Func1() is calling

True

请思考:将语句A a = new A()改成A a = new B() 或B a = new B(),对应的输出结果为多少?

当 A a = new A()改成A a = new B() 或B a = new B() 输出: B:Func1() is calling

B:Func2() is calling True

12、异常处理机制: try语句块中包含可能发生异常的代码,catch语句块中包含异常发生时的处理代码,finally语句块中的代码在有无异常的时候都会执行。 【例6】当参数为4和0时,以下程序的执行结果为 ( A ) public static void divide(int a, int b) { try { int c = a / b; } catch (Exception e) { Console.WriteLine (\ return; } finally { Console.WriteLine (\ } }

A. 输出 Exception Finally B. 输出 Finally C. 输出 Exception D. 无输出

【例7】下列代码中,哪些行将产生空指针异常。 ( A )

1 string s = null;

2 if ( s != null || s.Length > 0)

3 Console.WriteLine(\ 4 if ( s != null | s.Length > 0)

5 Console.WriteLine(\ 6 if ( s != null & s.Length > 0)

7 Console.WriteLine(\ 8 if ( s != null && s.Length > 0)

Console.WriteLine(\

A.2 4 6 B.2 4 6 8 C.4 6 8 D.2 6 8

13、单例设计模式:饿汉式和懒汉式。在单例设计模式中,懒汉式的代码可能会引发线程不同步的问题。

三、windows窗体设计 1、委托的定义及使用。 委托是一种数据结构,它提供类似C++语言中函数指针功能,委托可指静态方法和对象实例方法。它是完全的面向对象且使用完全类型,程序员可利用委托在执行时期传入方法名称,动态决定调用的方法。

使用:1.call back回调。2.定义准备被委托调用的方法。3.定义delegate类型的处理函数,并在此函数末。4.创建实例,传入准备调用的方法名。 2、C#使用____委托_____机制实现事件处理。 3、计时器控件。

【例1】在一个窗体应用程序中,需要在一分钟内自动浏览完文件夹中的60张图片,则需要设置计时器控件的Interval属性的值为 ( D )

A.10 B.600 C.100 D.1000

【例2】设有命令按钮btnText的单击事件代码如下,其中openFileDialog1为“打开文件对话框”对象,txtText为文本框对象。请阅读代码,并回答以下问题:(1)请简述该段代码的功能(2)请指出该段代码在执行过程中可能出现的问题,并给出解决办法。

private void btnText_Click(object sender, EventArgs e)

{

openFileDialog1.InitialDirectory = \

openFileDialog1.Filter = \文本文件 (*.txt)|*.txt \ openFileDialog1.Title = \打开文本文件\

if (openFileDialog1.ShowDialog() == DialogResult.OK) {

txtText.Text = File.ReadAllText(openFileDialog1.FileName); } }

(1) 功能:通过“打开文件”对话框openFileDialog选择文本文件,并将文本文件的内容显

示在txtText文本框中。

(2) 中文乱码问题。

Pvivate viod Fotml_Load(onject sdender, EvenArgs e) {

//设置文本框为多行文本 txtText.Multiline=true;

//为文本框添加垂直滚动条

txtTest.ScrollBars=ScrollBars.Vertical; }

Private void btnText_Click(object sender.EventArgs e) {

openFileDialog1.InitalDiretory=”c:\\\\”; openFileDialog1.Filter=”文本文件 (*.txt)|*.txt|全部文件(*.*)|*.*”

openFileDialog1.Title=”打开文本文件”;

if(openFileDialog1.ShowDialog()==DialogResult.OK) {

txtText.Text=File.ReadAllText(openFileDialog1.FileName, System.Text.Encoding.GetEncoding(“GBK”)); //在中文windows操作系统中等价于 txtText.Text

=File.ReadAllText(openFileDialog1.FileName,System.Text.Encoding.Default);

} }

四、数据库连接

1、在ADO.NET框架中,通过创建Connection对象建立与数据源之间的连接,通过创建Command对数据源执行各种SQL命令。

【例1】判断题:在ADO.NET框架中,连接SQL Server2005数据库与连接SQL Server Express2005数据库采用的连接符串不同。对

2、当只需要顺序读取数据库中的数据而不需要其他操作时,建议使用DataReader对象。 3、MD5算法。

思考:如何使用后台数据库中的userInfo(userName,userPass)表实现用户的登录与注册?其中,userPass字段中存放的是由MD5算法加密后的用户密码。 /*使用MD5加密字符串的方法*/

public static string GetMd5Str(string myString) {

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] fromData = Encoding.Unicode.GetBytes(myString); byte[] toData = md5.ComputeHash(fromData); string byteStr = null;

for (int i = 0; i < toData.Length; i++) {

byteStr += toData[i].ToString(\ }

return byteStr; }

/* btnAdd_Click 方法实现用户信息的注册*/

private void btnAdd_Click(object sender, EventArgs e) {

string userName = txtUser.Text.Trim(); string userPass = txtPass.Text.Trim();

string conStr = \ SqlConnection sqlCon = new SqlConnection(conStr); sqlCon.Open();

SqlCommand checkCmd = sqlCon.CreateCommand();

checkCmd.CommandText = \ checkCmd.Parameters.AddWithValue(\ SqlDataReader reader = checkCmd.ExecuteReader(); if (reader.Read()) {

MessageBox.Show(\用户名已经存在\ } else {