




免費預覽已結束,剩余4頁可下載查看
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、簡要回答下列問題。(1)舉例說明new關鍵字可用于哪些方面?(2)sealed關鍵字的作用是什么?什么情況下需要使用sealed關鍵字?(3)哪些關鍵字可以用于版本控制?【解答】1)在C#中,new關鍵字可用作運算符或修飾符。作為運算符用于在堆上創(chuàng)建對象和調用構造函數(shù)。作為修飾符用于隱藏基類成員的繼承成員。2) 在類聲明中使用sealed修飾符可防止其它類繼承此類。在方法聲明中使用sealed修飾符可防止擴充類重寫此方法。sealed修飾符主要用于防止非有意的派生,但是它還能促使某些運行時優(yōu)化。具體說來,由于密封類永遠不會有任何派生類,所以對密封類的實例的虛擬函數(shù)成員的調用可以轉換為非虛擬調用來處理。3) override關鍵字和new關鍵字均可用于版本控制。在C#中,默認情況下方法不是虛擬的。若要使方法成為虛擬方法,必須在基類的方法聲明中使用virtual修飾符。然后,派生類可以使用override關鍵字重寫基類中的虛擬方法,或使用new關鍵字隱藏基類中的虛擬方法。如果override關鍵字和new關鍵字均未指定,編譯器將發(fā)出警告,并且派生類中的方法將隱藏基類中的方法。2、簡要回答抽象類和接口的主要區(qū)別?!窘獯稹砍橄箢惡徒涌诘囊粋€主要差別是:類可以實現(xiàn)多個接口,但僅能從一個抽象類或任何其它類型的類繼承。3、編寫一個控制臺應用程序,完成下列功能,并回答提出的問題。(1)創(chuàng)建一個類A,在構造函數(shù)中輸出“A”,再創(chuàng)建一個類B,在構造函數(shù)中輸出“B”。(2)從A繼承一個名為C的新類,并在C內創(chuàng)建一個成員B。不要為C創(chuàng)建構造函數(shù)。(3)在Main方法中創(chuàng)建類C的一個對象,寫出運行程序后輸出的結果。(4)如果在C中也創(chuàng)建一個構造函數(shù)輸出“C”,整個程序運行的結果又是什么?【解答】using System;public class A public A() Console.WriteLine(A); public class B public B() Console.WriteLine(B); public class C : A B newb = new B();class MainClass public static void Main() C newc = new C(); Console.ReadLine(); 輸出結果:BA 如果在C中也創(chuàng)建一個構造函數(shù)輸出“C”,即添加:public C()Console.WriteLine(C); 則整個程序運行的結果為:BAC4、編寫一個控制臺應用程序,完成下列功能,并寫出運行程序后輸出的結果。(1)創(chuàng)建一個類A,在A中編寫一個可以被重寫的帶int類型參數(shù)的方法MyMethod,并在該方法中輸出傳遞的整型值加10后的結果。(2)再創(chuàng)建一個類B,使其繼承自類A,然后重寫A中的MyMethod方法,將A中接收的整型值加50,并輸出結果。(3)在Main方法中分別創(chuàng)建類A和類B的對象,并分別調用MyMethod方法?!窘獯稹縰sing System;public class A public virtual void MyMethod(int num) num += 10; Console.WriteLine(num); public class B : A public override void MyMethod(int num) num += 50; Console.WriteLine(num); class MainClass public static void Main() A newa = new A(); newa.MyMethod(2); B newb = new B(); newb.MyMethod(2); Console.ReadLine(); 輸出結果:12525、假設Node類的每一個節(jié)點包括有兩個字段:m_data(引用節(jié)點的數(shù)據(jù))和m_next(引用鏈接列表中的下一項)。這兩個字段都是由構造函數(shù)方法設置的。該類有兩個功能,第一個功能是通過名為Data和Next的只讀屬性訪問m_data和m_next字段。第二個功能是對System.Object的ToString虛擬方法進行重寫。試分別用類和泛型兩種方法編寫程序實現(xiàn)上述功能?!窘獯稹縰sing System;class Node Object m_data; Node m_next; public Node(Object data, Node next) m_data = data; m_next = next; / 訪問結點數(shù)據(jù) public Object Data get return m_data; / 訪問下一個結點 public Node Next get return m_next; / 獲取結點數(shù)據(jù)描述 public override String ToString() return m_data.ToString(); / 鏈表結點類的泛型定義class Node T m_data; Node m_next; public Node(T data, Node next) m_data = data; m_next = next; / 訪問結點數(shù)據(jù) public T Data get return m_data; set m_data = value; / 訪問下一個結點 public Node Next get return m_next; set m_next = value; / 獲取結點數(shù)據(jù)描述 public override String ToString() return m_data.ToString(); / 使用結點類型或泛型結點類型class LinkedList static void Main(string args) / 創(chuàng)建整數(shù)鏈表 /Node head = new Node(5, null); /head = new Node(10, head); /head = new Node(15, head); /遍歷鏈表求整數(shù)和 /Int32 sum = 0; /for (Node current = head; current != null; / current = current.Next) / / sum += (Int32)current.Data; / / 輸出結果 /Console.WriteLine(Sum of nodes = 0, sum); / 用泛型創(chuàng)建整數(shù)鏈表 Node head = new Node(5, null); head = new Node(10, head); head = new Node(15, head); / 遍歷求和 Int32 sum = 0; for (Node current = head; current != null; current = current.Next) sum += current.Data; / 輸出 Console.WriteLine(Sum of nodes = 0, sum.ToString(); 1、使用保持連接方式編寫程序,計算各年級平均成績,并顯示結果?!窘獯稹縰sing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 習題9_1 public partial class Form1 : Form public Form1() InitializeComponent(); /添加Button按鈕在ListBox中顯示結果 private void button1_Click(object sender, EventArgs e) listBox1.Items.Add(年級 平均成績); string connectionString = Properties.Settings.Default.MyDatabaseConnectionString; /根據(jù)連接字符串創(chuàng)建SqlConnection實例 SqlConnection conn = new SqlConnection(connectionString); /創(chuàng)建SqlCommand實例,并設置SQL語句和使用的連接實例 SqlCommand cmd = new SqlCommand(); cmd.CommandText = select substring(學號,1,2) as 年級,avg(成績) as 平均成績 from MyTable2 group by substring(學號,1,2); cmd.Connection = conn; try conn.Open(); SqlDataReader r = cmd.ExecuteReader(); while (r.Read() = true) listBox1.Items.Add(string.Format(0級 1, r0, r1); r.Close(); catch (Exception err) MessageBox.Show(err.Message, 計算成績失敗); finally conn.Close(); 2、使用保持連接方式編寫程序,查詢MyTable2中不及格學生的學號、姓名、性別和成績。并將結果在ListBox中顯示出來?!窘獯稹縰sing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 習題9_2 public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) listBox1.Items.Add( 學號 姓名 性別 成績); string connectionString = Properties.Settings.Default.MyDatabaseConnectionString; /根據(jù)連接字符串創(chuàng)建SqlConnection實例 SqlConnection conn = new SqlConnection(connectionString); /創(chuàng)建SqlCommand實例,并設置SQL語句和使用的連接實例 SqlCommand cmd = new SqlCommand(); cmd.CommandText = Select 學號,姓名,性別, 成績 From MyTable2 Where (成績 18 , conn); DataSet dataset = new DataSet(); adapter.Fill(dataset, person); dataGridView1.DataSource = dataset.Tablesperson; adapter.Dispose(); catch (Exception err) MessageBox.Show(err.Message); finally conn.Close(); 5、調用存儲過程,設計程序完成下列功能:任意給出一個漢字,統(tǒng)計MyTable2中所有包含該漢字的人數(shù),并顯示統(tǒng)計結果?!窘獯稹縰sing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 習題9_5 public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyDatabaseConnectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; /設置S
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 培訓學校新學期動員大會
- 2025年制造業(yè)供應鏈數(shù)字化協(xié)同管理下的供應鏈金融創(chuàng)新模式研究報告001
- 藥品研發(fā)與生產知識產權保護策略研究報告001
- 藥品研發(fā)合作模式與案例分析報告
- 基于大數(shù)據(jù)分析的2025年互聯(lián)網+政務服務風險防控報告
- 2025年智慧能源管理系統(tǒng)建設方案:能源行業(yè)物聯(lián)網解決方案報告
- 市場合作營銷合同書文檔
- 現(xiàn)任高管在職證明及職責描述(6篇)
- 2025年天然氣汽車泄漏報警器項目提案報告
- 股東股權份額與出資證明書(5篇)
- 工地交通安全
- 【MOOC】多媒體技術與應用-同濟大學 中國大學慕課MOOC答案
- 高等數(shù)學基礎-002-國開機考復習資料
- 各氣象臺站區(qū)站號
- 2024年1月云南高中學業(yè)水平考試數(shù)學試卷真題(含答案詳解)
- 高教版2023年中職教科書《語文》(基礎模塊)下冊教案全冊
- 高績效教練讀書分享
- 2024年度xx村監(jiān)測對象風險消除民主評議會議記錄
- 水利工程外觀質量評定標準DB41-T 1488-2017
- 【高分復習資料】山東大學《244德語》歷年考研真題匯編
- 中、小學文件材料分類方案、歸檔范圍、保管期限表(三合一制度)
評論
0/150
提交評論